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

The following examples show how to use org.openide.util.RequestProcessor#submit() . 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 testCancelDoesNotInterruptIfNotPassedToFutureDotCancel() throws Exception {
    RequestProcessor rp = new RequestProcessor ("X", 3, false, true);
    final CountDownLatch releaseForRun = new CountDownLatch(1);
    final CountDownLatch enterLatch = new CountDownLatch(1);
    final CountDownLatch exitLatch = new CountDownLatch(1);
    class R implements Runnable {
        volatile boolean interrupted;
        @Override
        public void run() {
            enterLatch.countDown();
            try {
                releaseForRun.await();
            } catch (InterruptedException ex) {
                interrupted = true;
            }
            interrupted |= Thread.interrupted();
            exitLatch.countDown();
        }
    }
    R r = new R();
    Future<?> f = rp.submit(r);
    enterLatch.await();
    f.cancel(false);
    assertTrue (f.isCancelled());
    assertFalse (r.interrupted);
}
 
Example 2
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 3
Source File: ProcessUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Starts executable and returns immediately. 
 * @param execEnv - target execution environment
 * @param rp - RequestProcessor that is used for running the task. 
 * Could be NULL. In this case default (private) processor is used.
 * Note that default (private) processor has throughput == 1
 * @param postExecutor - once process is done, passed postExecutor will be 
 * notified. Call of postExecutor's method is performed in the same thread 
 * as invocation of the executable (i.e. in rp (see above)).
 * @param executable - full path to executable to run.
 * @param args - list of arguments to pass to executable
 * @return Future ExitStatus
 */
public static Future<ExitStatus> execute(final ExecutionEnvironment execEnv, final RequestProcessor rp, final PostExecutor postExecutor, final String executable, final String... args) {
    final RequestProcessor processor = (rp == null) ? RP : rp;
    return processor.submit(new Callable<ExitStatus>() {

        @Override
        public ExitStatus call() throws Exception {

            ExitStatus status = null;
            String error = null;

            try {
                status = execute(execEnv, executable, args);
            } catch (Throwable t) {
                error = t.getMessage();
            } finally {
                if (postExecutor != null) {
                    postExecutor.processFinished(error == null ? status : new ExitStatus(1, null, Arrays.asList(error.split("\n")))); // NOI18N
                }
            }

            return status;
        }
    });
}
 
Example 4
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testCancelFutureInterruptsThreadEvenIfRequestProcessorForbidsIt() throws Exception {
    RequestProcessor rp = new RequestProcessor ("X", 3, false, true);
    final CountDownLatch releaseForRun = new CountDownLatch(1);
    final CountDownLatch enterLatch = new CountDownLatch(1);
    final CountDownLatch exitLatch = new CountDownLatch(1);
    class R implements Runnable {
        volatile boolean interrupted;
        @Override
        public void run() {
            enterLatch.countDown();
            try {
                releaseForRun.await();
            } catch (InterruptedException ex) {
                interrupted = true;
            }
            interrupted |= Thread.interrupted();
            exitLatch.countDown();
        }
    }
    R r = new R();
    Future<?> f = rp.submit(r);
    enterLatch.await();
    f.cancel(true);
    assertTrue (f.isCancelled());
    exitLatch.await();
    assertTrue (r.interrupted);
}
 
Example 5
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAwaitingTasksReturnedOnShutdownNow() throws InterruptedException {
    final CountDownLatch startupLatch = new CountDownLatch(2);
    final CountDownLatch blockingLatch = new CountDownLatch(1);
    Runnable blockingRunnable = new Runnable() {

        @Override
        public void run() {
            try {
                startupLatch.countDown();
                startupLatch.await();
                blockingLatch.await();
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    };
    
    Runnable awaitingRunnable = new Runnable() {

        @Override
        public void run() {
            // noop
        }
    };
    
    RequestProcessor rp = new RequestProcessor("X", 1);
    rp.submit(blockingRunnable);
    startupLatch.countDown();
    startupLatch.await();
    rp.submit(awaitingRunnable);
    Set<Runnable> awaiting = new HashSet<Runnable>(rp.shutdownNow());
    assertTrue("Awaiting task not returned on shutdownNow()", awaiting.contains(awaitingRunnable));
    assertFalse("Running task returned on shutdownNow()", awaiting.contains(blockingRunnable));
}
 
Example 6
Source File: NativeProcessBuilderTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@ForAllEnvironments(section = "remote.platforms")
public void testListeners() throws Exception {
    int count = 5;
    RequestProcessor rp = new RequestProcessor("testListeners", 50);
    final CountDownLatch done = new CountDownLatch(count * 4);
    final CountDownLatch start = new CountDownLatch(1);

    for (int i = 0; i < count * 4; i++) {
        final int x = i;
        rp.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    start.await();

                    switch (x % 4) {
                        case 0:
                            doTestListeners(getTestExecutionEnvironment(), true, true);
                            break;
                        case 1:
                            doTestListeners(getTestExecutionEnvironment(), true, false);
                            break;
                        case 2:
                            doTestListeners(getTestExecutionEnvironment(), false, true);
                            break;
                        case 3:
                            doTestListeners(getTestExecutionEnvironment(), false, false);
                            break;

                    }
                } catch (Throwable ex) {
                    Exceptions.printStackTrace(ex);
                } finally {
                    done.countDown();
                }
            }
        });
    }

    start.countDown();
    done.await();
}