Example usage for java.lang Runnable run

List of usage examples for java.lang Runnable run

Introduction

In this page you can find the example usage for java.lang Runnable run.

Prototype

public abstract void run();

Source Link

Document

When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

Usage

From source file:com.fdwills.external.http.AsyncHttpResponseHandler.java

/**
 * Helper method to send runnable into local handler loop
 *
 * @param runnable runnable instance, can be null
 *//*w w w . jav a2  s.com*/
protected void postRunnable(Runnable runnable) {
    if (runnable != null) {
        if (getUseSynchronousMode() || handler == null) {
            // This response handler is synchronous, run on current thread
            runnable.run();
        } else {
            // Otherwise, run on provided handler
            AssertUtils.asserts(handler != null, "handler should not be null!");
            handler.post(runnable);
        }
    }
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.internal.concurrent.BoundedExecutor.java

/**
 * Executes the task to the configured executor immediately if the executor
 * is not too busy (is already executing the number of commands equal to the
 * configured bounds for this instance), otherwise block.
 *
 * @param command//  w  w w. j a v  a  2 s . c o m
 *        the command to submit (must not be <code>null</code>)
 * @throws InterruptedException
 *         if the thread is interrupted while waiting for the semaphore that
 *         controls access to this executor.
 */
@Override
public void execute(final Runnable command) {
    Check.notNull(command, "command"); //$NON-NLS-1$

    try {
        acquire();
    } catch (final InterruptedException e) {
        log.warn("Interrupted waiting on semaphore; re-interrupting current thread", e); //$NON-NLS-1$
        // Re-interrupt the current thread.
        Thread.currentThread().interrupt();
    }

    try {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    command.run();
                } finally {
                    release();
                }
            }
        });
    } catch (final RejectedExecutionException e) {
        release();
    }
}

From source file:Parallelizer.java

/**
 * Run the given job.  The given job is either run
 * immediately or if the max number of concurrent jobs are already
 * running, it is queued to be run when some job is finished.
 * <p>/*from  www . ja  va2  s . co  m*/
 * If this method throws an error, that
 * error may be handled and this method
 * may be called again as it will not re-throw the same
 * instance of the error.
 *
 * @param threadGroup group in which this job should be run (null for default group).
 * @param job job which is to be run in parallel with other jobs.
 * @param threadName name for the thread that will be created to run the job (null for auto generated thread name)
 * @param stackSize system dependent stack size suggestion for thread creation (0 for default stack size).
 * @throws Error if any thread that is already running has thrown an Error.
 * @throws NullPointerException if job is null.
 *
 * @since ostermillerutils 1.05.00
 */
public void run(ThreadGroup threadGroup, final Runnable job, String threadName, long stackSize) {
    throwFirstError();

    Runnable jobWrapper = new Runnable() {
        public void run() {
            try {
                job.run();
            } catch (RuntimeException runtimeException) {
                // Put exceptions in the exception queue
                synchronized (runningThreads) {
                    exceptionList.add(runtimeException);
                }
            } catch (Error error) {
                // Put errors in the error queue
                synchronized (runningThreads) {
                    errorList.add(error);
                }
            } finally {
                synchronized (runningThreads) {
                    // when done remove ourselves from the list
                    // of running threads.
                    runningThreads.remove(Thread.currentThread());
                    // Notify the block method.
                    runningThreads.notifyAll();
                }
                // If there are jobs queued up to be run, now would
                // be a good time to run them.
                startAJobIfNeeded();
            }
        }
    };

    // ensure the thread name is not null, and auto generate a name if it is
    threadName = getNextThreadName(threadName);

    // If we are already running the max number of jobs, queue this job up
    synchronized (runningThreads) {
        toRunQueue.add(new Thread(threadGroup, jobWrapper, threadName, stackSize));
    }

    // Now that the job is in the queue of jobs to run,
    // check the queue and see if the job should be started
    startAJobIfNeeded();
}

From source file:com.intellij.lang.jsgraphql.languageservice.JSGraphQLNodeLanguageServiceInstance.java

private void createProcessHandler() {

    // Make sure we have a node interpreter
    final String nodeInterpreter = getNodeInterpreter(project);
    if (nodeInterpreter == null) {
        if (log.isDebugEnabled()) {
            log.debug("Can't create process handler: No Node.js interpreter configured.");
        }//from w  ww .  ja v a2s.co  m
        return;
    }

    try {

        if (log.isDebugEnabled()) {
            log.debug("Resolving node.js file...");
        }

        final File jsGraphQLNodeFile = getOrCreateJSGraphQLLanguageServiceFileName();
        if (jsGraphQLNodeFile == null) {
            if (log.isDebugEnabled()) {
                log.debug(
                        "Can't create process handler: Got null from getOrCreateJSGraphQLLanguageServiceFileName.");
            }
            return;
        }

        final int socketPort = NetUtils.findAvailableSocketPort();
        final GeneralCommandLine commandLine = new GeneralCommandLine(nodeInterpreter);

        commandLine.withWorkDirectory(jsGraphQLNodeFile.getParent());
        commandLine.addParameter(jsGraphQLNodeFile.getAbsolutePath());

        commandLine.addParameter("--port=" + socketPort);

        if (log.isDebugEnabled()) {
            log.debug("Creating processHandler using command line " + commandLine.toString());
        }

        processHandler = new OSProcessHandler(commandLine);
        JSGraphQLLanguageUIProjectService languageService = JSGraphQLLanguageUIProjectService
                .getService(project);
        final Runnable onInitialized = languageService.connectToProcessHandler(processHandler);

        if (waitForListeningNotification(processHandler, project)) {
            url = new URL("http", NetUtils.getLocalHostString(), socketPort,
                    JSGRAPHQL_LANGUAGE_SERVICE_MAPPING);
            onInitialized.run();
        } else {
            log.error("Unable to start JS GraphQL Language Service using Node.js with commandline "
                    + commandLine.toString());
        }

    } catch (IOException | ExecutionException e) {
        log.error("Error running JS GraphQL Language Service using Node.js", e);
    }
}

From source file:org.opendatakit.briefcase.ui.settings.SettingsPanelForm.java

private FocusListener onFocusLost(Runnable callback) {
    return new FocusAdapter() {
        @Override/* www . j  a v a 2  s.  co m*/
        public void focusLost(FocusEvent e) {
            super.focusLost(e);
            callback.run();
        }
    };
}

From source file:com.example.pierre.applicompanies.library_http.AsyncHttpResponseHandler.java

/**
 * Helper method to send runnable into local handler loop
 *
 * @param runnable runnable instance, can be null
 *//*  w  ww  .  j  a va2 s  .  c  o m*/
protected void postRunnable(Runnable runnable) {
    if (runnable != null) {
        if (getUseSynchronousMode() || handler == null) {
            // This response handler is synchronous, run on current thread
            runnable.run();
        } else {
            // Otherwise, run on provided handler
            Utils.asserts(handler != null, "handler should not be null!");
            handler.post(runnable);
        }
    }
}

From source file:org.ehoffman.aopalliance.extensions.scope.SpringTestScope.java

@Override
public Object remove(final String name) {
    Object removed = null;/*from w ww.j  a  va  2  s. c  o m*/
    if (exists(name)) {
        final Runnable callback = destroyCallbacks.get().remove(name);
        removed = values.get().remove(name);
        LOGGER.info("removing " + name + " but will remove " + dependsOnMe(name) + " first");
        for (final String dependensOnTarget : dependsOnMe(name)) {
            if (exists(dependensOnTarget)) {
                remove(dependensOnTarget);
            }
        }
        try {
            if (callback != null) {
                callback.run();
            }
        } catch (final Throwable t) {
            LOGGER.error("running of callback failed for bean of name: " + name, t);
        }
    }
    return removed;
}

From source file:cn.com.loopj.android.http.AsyncHttpResponseHandler.java

/**
 * Helper method to send runnable into local handler loop
 *
 * @param runnable runnable instance, can be null
 *//*ww  w.  j  ava2  s .  c  om*/
protected void postRunnable(Runnable runnable) {
    if (runnable != null) {
        if (getUseSynchronousMode() || handler == null) {
            // This response handler is synchronous, run on current thread
            runnable.run();
        } else {
            // Otherwise, run on provided handler
            handler.post(runnable);
        }
    }
}

From source file:com.nbt.TileCanvas.java

public void doRepaint() {
    Runnable runnable = new Runnable() {
        @Override//from  w  w  w .ja  va  2 s . c  o m
        public void run() {
            repaint();
        }
    };
    if (SwingUtilities.isEventDispatchThread())
        runnable.run();
    else
        SwingUtilities.invokeLater(runnable);
}

From source file:dstrelec.nats.listener.DefaultNatsListenerContainer.java

protected void doStop(final Runnable callback) {
    if (isRunning()) {
        for (AsyncSubscription subscription : subscriptions) {
            //subscription.unsubscribe();
            if (logger.isDebugEnabled()) {
                logger.debug("Unsubscribed from subject " + subscription.getSubject());
            }/*from ww  w  .ja  v  a 2  s .  c  o m*/
        }

        if (connection != null) {
            connection.close();
        }

        if (callback != null) {
            callback.run();
        }

        this.running = false;
    }
}