Java TimeUnit Usage run(Callable task, long timeout)

Here you can find the source of run(Callable task, long timeout)

Description

run

License

Open Source License

Declaration

public static <T> T run(Callable<T> task, long timeout) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class Main {
    public static <T> T run(Callable<T> task, long timeout) throws Exception {
        FutureTask<T> future = new FutureTask<T>(task);
        new Thread(future).start();
        T r = null;//from   www.ja v a 2  s.  c o m
        try {
            r = future.get(timeout, TimeUnit.MILLISECONDS);
        } finally {
            try {
                future.cancel(true);
            } catch (Exception ex) {
            }
        }
        return r;
    }
}

Related

  1. printTimeString(long time)
  2. printTiming(long start, long end)
  3. randomNanos(Random rand, int decimalDigits)
  4. rangeOf(final Date date, final int beforeDates, final int afterDates)
  5. roundTimestampToNexDay(long timestamp)
  6. second(long second)
  7. seconds(long timeInMillis)
  8. secondsFromNow(long seconds)
  9. secondsPassed(int startTime)