Search in sources :

Example 36 with FutureTask

use of java.util.concurrent.FutureTask in project indy by Commonjava.

the class ThreadDumper method timeoutRule.

public static TestRule timeoutRule(int timeout, TimeUnit units) {
    return (base, description) -> new Statement() {

        public void evaluate() throws Throwable {
            System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
            AtomicReference<Throwable> error = new AtomicReference<>();
            CountDownLatch latch = new CountDownLatch(1);
            FutureTask<Void> task = new FutureTask<>(() -> {
                try {
                    latch.countDown();
                    base.evaluate();
                } catch (Throwable t) {
                    error.set(t);
                }
                return null;
            });
            ThreadGroup tg = new ThreadGroup("Test Timeout Group");
            Thread t = new Thread(tg, task, "Test Timeout Thread");
            t.setDaemon(true);
            t.start();
            try {
                System.out.println("Waiting for test to start.");
                latch.await();
            } catch (InterruptedException e) {
                error.set(e);
            }
            if (error.get() == null) {
                try {
                    System.out.println("Waiting for test to complete (or timeout)");
                    task.get(timeout, units);
                } catch (InterruptedException e) {
                    error.set(e);
                } catch (ExecutionException e) {
                    error.set(e.getCause());
                } catch (TimeoutException e) {
                    System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
                    dumpThreads();
                    StackTraceElement[] stackTrace = t.getStackTrace();
                    Exception currThreadException = new TestTimedOutException(timeout, units);
                    if (stackTrace != null) {
                        currThreadException.setStackTrace(stackTrace);
                        t.interrupt();
                    }
                    throw currThreadException;
                }
            }
            Throwable throwable = error.get();
            if (throwable != null) {
                throw throwable;
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) TestRule(org.junit.rules.TestRule) MonitorInfo(java.lang.management.MonitorInfo) TestTimedOutException(org.junit.runners.model.TestTimedOutException) FutureTask(java.util.concurrent.FutureTask) TimeoutException(java.util.concurrent.TimeoutException) ThreadMXBean(java.lang.management.ThreadMXBean) StringUtils.join(org.apache.commons.lang.StringUtils.join) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadInfo(java.lang.management.ThreadInfo) Stream(java.util.stream.Stream) ManagementFactory(java.lang.management.ManagementFactory) Statement(org.junit.runners.model.Statement) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) FutureTask(java.util.concurrent.FutureTask) ExecutionException(java.util.concurrent.ExecutionException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 37 with FutureTask

use of java.util.concurrent.FutureTask in project jdk8u_jdk by JetBrains.

the class CThreading method executeOnAppKit.

public static <V> V executeOnAppKit(Callable<V> command) throws Throwable {
    if (!isAppKit()) {
        Dispatch dispatch = Dispatch.getInstance();
        if (dispatch == null) {
            throw new AWTError("Could not get Dispatch object");
        }
        FutureTask<V> future = new FutureTask<>(command);
        dispatch.getNonBlockingMainQueueExecutor().execute(future);
        try {
            return future.get();
        } catch (InterruptedException e) {
            throw new AWTError(e.getMessage());
        } catch (ExecutionException e) {
            throw e.getCause();
        }
    } else
        return command.call();
}
Also used : FutureTask(java.util.concurrent.FutureTask) Dispatch(com.apple.concurrent.Dispatch) AWTError(java.awt.AWTError) ExecutionException(java.util.concurrent.ExecutionException)

Example 38 with FutureTask

use of java.util.concurrent.FutureTask in project indy by Commonjava.

the class ThreadDumper method timeoutRule.

public static TestRule timeoutRule(int timeout, TimeUnit units) {
    return (base, description) -> new Statement() {

        public void evaluate() throws Throwable {
            System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
            AtomicReference<Throwable> error = new AtomicReference<>();
            CountDownLatch latch = new CountDownLatch(1);
            FutureTask<Void> task = new FutureTask<>(() -> {
                try {
                    latch.countDown();
                    base.evaluate();
                } catch (Throwable t) {
                    error.set(t);
                }
                return null;
            });
            ThreadGroup tg = new ThreadGroup("Test Timeout Group");
            Thread t = new Thread(tg, task, "Test Timeout Thread");
            t.setDaemon(true);
            t.start();
            try {
                System.out.println("Waiting for test to start.");
                latch.await();
            } catch (InterruptedException e) {
                error.set(e);
            }
            if (error.get() == null) {
                try {
                    System.out.println("Waiting for test to complete (or timeout)");
                    task.get(timeout, units);
                } catch (InterruptedException e) {
                    error.set(e);
                } catch (ExecutionException e) {
                    error.set(e.getCause());
                } catch (TimeoutException e) {
                    System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
                    dumpThreads();
                    StackTraceElement[] stackTrace = t.getStackTrace();
                    Exception currThreadException = new TestTimedOutException(timeout, units);
                    if (stackTrace != null) {
                        currThreadException.setStackTrace(stackTrace);
                        t.interrupt();
                    }
                    throw currThreadException;
                }
            }
            Throwable throwable = error.get();
            if (throwable != null) {
                throw throwable;
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) TestRule(org.junit.rules.TestRule) MonitorInfo(java.lang.management.MonitorInfo) TestTimedOutException(org.junit.runners.model.TestTimedOutException) FutureTask(java.util.concurrent.FutureTask) TimeoutException(java.util.concurrent.TimeoutException) ThreadMXBean(java.lang.management.ThreadMXBean) StringUtils.join(org.apache.commons.lang.StringUtils.join) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadInfo(java.lang.management.ThreadInfo) Stream(java.util.stream.Stream) ManagementFactory(java.lang.management.ManagementFactory) Statement(org.junit.runners.model.Statement) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) FutureTask(java.util.concurrent.FutureTask) ExecutionException(java.util.concurrent.ExecutionException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 39 with FutureTask

use of java.util.concurrent.FutureTask in project android_frameworks_base by crdroidandroid.

the class MffTestCase method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // MffContext needs to be created on a separate thread to allow MFF to post Runnable's.
    mMffContextHandlerThread = new HandlerThread("MffContextThread");
    mMffContextHandlerThread.start();
    Handler handler = new Handler(mMffContextHandlerThread.getLooper());
    FutureTask<MffContext> task = new FutureTask<MffContext>(new Callable<MffContext>() {

        @Override
        public MffContext call() throws Exception {
            MffContext.Config config = new MffContext.Config();
            config.requireCamera = false;
            config.requireOpenGL = false;
            config.forceNoGL = true;
            return new MffContext(getContext(), config);
        }
    });
    handler.post(task);
    // Wait for the context to be created on the handler thread.
    mMffContext = task.get();
}
Also used : HandlerThread(android.os.HandlerThread) FutureTask(java.util.concurrent.FutureTask) Handler(android.os.Handler)

Example 40 with FutureTask

use of java.util.concurrent.FutureTask in project double-espresso by JakeWharton.

the class AsyncTaskPoolMonitorTest method testIdleNotificationAndRestart.

public void testIdleNotificationAndRestart() throws Exception {
    FutureTask<Thread> workerThreadFetchTask = new FutureTask<Thread>(new Callable<Thread>() {

        @Override
        public Thread call() {
            return Thread.currentThread();
        }
    });
    testThreadPool.submit(workerThreadFetchTask);
    Thread workerThread = workerThreadFetchTask.get();
    final CountDownLatch runLatch = new CountDownLatch(1);
    final CountDownLatch exitLatch = new CountDownLatch(1);
    testThreadPool.submit(new Runnable() {

        @Override
        public void run() {
            runLatch.countDown();
            try {
                exitLatch.await();
            } catch (InterruptedException ie) {
                throw new RuntimeException(ie);
            }
        }
    });
    assertTrue(runLatch.await(1, TimeUnit.SECONDS));
    final CountDownLatch notificationLatch = new CountDownLatch(1);
    monitor.notifyWhenIdle(new Runnable() {

        @Override
        public void run() {
            notificationLatch.countDown();
        }
    });
    // give some time for the idle detection threads to spin up.
    Thread.sleep(2000);
    // interrupt one of them
    workerThread.interrupt();
    Thread.sleep(1000);
    // unblock the dummy work item.
    exitLatch.countDown();
    assertTrue(notificationLatch.await(1, TimeUnit.SECONDS));
    assertTrue(monitor.isIdleNow());
}
Also used : FutureTask(java.util.concurrent.FutureTask) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

FutureTask (java.util.concurrent.FutureTask)126 ExecutionException (java.util.concurrent.ExecutionException)44 Test (org.junit.Test)32 IOException (java.io.IOException)28 ExecutorService (java.util.concurrent.ExecutorService)23 Callable (java.util.concurrent.Callable)22 CountDownLatch (java.util.concurrent.CountDownLatch)20 TimeoutException (java.util.concurrent.TimeoutException)14 Handler (android.os.Handler)12 ArrayList (java.util.ArrayList)12 CancellationException (java.util.concurrent.CancellationException)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 Future (java.util.concurrent.Future)8 AccessibleObject (java.lang.reflect.AccessibleObject)6 List (java.util.List)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 FileNotFoundException (java.io.FileNotFoundException)5 InputStream (java.io.InputStream)5 Iterator (java.util.Iterator)5 CancellationSignal (android.os.CancellationSignal)4