Java Code Examples for org.openide.util.RequestProcessor#shutdown()

The following examples show how to use org.openide.util.RequestProcessor#shutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testSubmittedTasksExecutedBeforeShutdown() throws InterruptedException {
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch executedLatch = new CountDownLatch(2);
    Runnable dummyRunnable = new Runnable() {

        @Override
        public void run() {
            try {
                startLatch.await();
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            } finally {
                executedLatch.countDown();
            }
        }
    };
    
    RequestProcessor rp = new RequestProcessor("X", 1);
    rp.submit(dummyRunnable);
    rp.submit(dummyRunnable);
    rp.shutdown();
    startLatch.countDown();
    
    assertTrue("Submitted tasks not executed", executedLatch.await(5, TimeUnit.SECONDS));
}
 
Example 2
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testAwaitTermination() throws Exception {
    int count = 20;
    final Object lock = new Object();
    final CountDownLatch waitAllLaunched = new CountDownLatch(count);
    final CountDownLatch waitAll = new CountDownLatch(count);
    final RequestProcessor rp = new RequestProcessor("TestRP", count);
    class R implements Runnable {

        volatile boolean hasStarted;
        volatile boolean hasFinished;

        @Override
        public void run() {
            hasStarted = true;
            waitAllLaunched.countDown();
            synchronized (lock) {
                try {
                    lock.wait();
                    if (Thread.interrupted()) {
                        return;
                    }
                } catch (InterruptedException ex) {
                    return;
                } finally {
                    hasFinished = true;
                    waitAll.countDown();
                }
            }
        }
    }
    Set<Future<String>> s = new HashSet<Future<String>>();
    Set<R> rs = new HashSet<R>();
    for (int i = 0; i < count; i++) {
        String currName = "Runnable " + i;
        R r = new R();
        rs.add(r);
        s.add(rp.submit(r, currName));
    }
    waitAllLaunched.await();
    synchronized (lock) {
        //Notify just one thread
        lock.notifyAll();
    }
    rp.shutdown();
    boolean awaitTermination = rp.awaitTermination(1, TimeUnit.DAYS);
    assertTrue(awaitTermination);
    assertTrue(rp.isShutdown());
    assertTrue(rp.isTerminated());
}