Java 8 ExecutorService - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 ExecutorService class.

This code example shows how to use the following methods:awaitTermination, shutdown, shutdownNow
        IoUtils.safeClose(serverChannel);
    } 
 
    public void shutdownRemoting() throws IOException, InterruptedException { 
        channelServer.close();
        executorService.shutdown();
        executorService.awaitTermination(1L, TimeUnit.DAYS);
        executorService.shutdownNow();
    } 
 
} 
8
CodeRank
This code example shows how to use the following methods:awaitTermination, shutdown, shutdownNow
      throws InterruptedException { 
    if (service == null) {
      return true; 
    } 
 
    service.shutdown();
    if (!service.awaitTermination(timeoutInMs, TimeUnit.MILLISECONDS)) {
      service.shutdownNow();
      return service.awaitTermination(timeoutInMs, TimeUnit.MILLISECONDS);
    } else { 
      return true; 
    } 
  } 
} 
8
CodeRank
This code example shows how to use the following methods:shutdown, shutdownNow
        return executor;
    } 
 
    @Override 
    public void close() throws IOException { 
        executor.shutdown();
        try { 
            endpoint.close();
        } finally { 
            executor.shutdownNow();
        } 
    } 
 
 
} 
8
CodeRank
This code example shows how to use the following methods:execute, isShutdown, isTerminated, shutdown
        assertEquals("Should use same thread", name1, name2);
    } 
 
    public void testSynchronousExecutorServiceShutdown() throws Exception {
        ExecutorService service = new SynchronousExecutorService();
        service.execute(new Runnable() {
            public void run() { 
                invoked = true;
            } 
        }); 
        service.shutdown();
 
        assertTrue(service.isShutdown());
        assertTrue(service.isTerminated());
    } 
} 
7
CodeRank
This code example shows how to use the following methods:shutdown, shutdownNow
    } 
 
    @Override 
    public void close() throws IOException { 
        if(destroyExecutor) {
            executorService.shutdown();
        } 
        if(endpoint != null) try {
            endpoint.close();
        } catch (IOException e) {
            // ignore 
        } 
        if(destroyExecutor) {
            executorService.shutdownNow();
        } 
    } 
 
    public static DomainControllerClientConfig create() throws IOException { 
        return create(createDefaultExecutor(), true); 
    } 
8
CodeRank
This code example shows how to use the following methods:awaitTermination, shutdown, shutdownNow
		} 
		if (this.waitForTasksToCompleteOnShutdown) {
			this.executor.shutdown();
		} 
		else { 
			this.executor.shutdownNow();
		} 
		awaitTerminationIfNecessary(); 
	} 
 
	/** 
	 * Wait for the executor to terminate, according to the value of the 
	 * {@link #setAwaitTerminationSeconds "awaitTerminationSeconds"} property. 
	 */ 
	private void awaitTerminationIfNecessary() { 
		if (this.awaitTerminationSeconds > 0) {
			try { 
				if (!this.executor.awaitTermination(this.awaitTerminationSeconds, TimeUnit.SECONDS)) {
					if (logger.isWarnEnabled()) {
						logger.warn("Timed out while waiting for executor" +

7
CodeRank
This code example shows how to use the following methods:shutdownNow
        this.executor = Executors.newCachedThreadPool(this.threadFactory);
    } 
 
    @Override 
    public void stop(StopContext context) {
        this.executor.shutdownNow();
    } 
} 
7
CodeRank
This code example shows how to use the following methods:submit
        final DataInputStream input = new DataInputStream(inputStream);
        // read the invocation id 
        final short invocationId = input.readShort();
        final String txParentNodeName = input.readUTF();
        final int recoveryFlags = input.readInt();
        executorService.submit(new TxRecoveryTask(channelAssociation, invocationId, txParentNodeName, recoveryFlags));
    } 
 
    private final class TxRecoveryTask implements Runnable {
 
        private final String txParentNodeName;
        private final short invocationId;
        private final ChannelAssociation channelAssociation;
        private final int recoveryFlags;
 
        TxRecoveryTask(final ChannelAssociation channelAssociation, final short invocationId, final String txParentNodeName, final int recoveryFlags) {
            this.txParentNodeName = txParentNodeName;
            this.invocationId = invocationId;
            this.channelAssociation = channelAssociation;
            this.recoveryFlags = recoveryFlags;
7
CodeRank
This code example shows how to use the following methods:awaitTermination, shutdown, shutdownNow
	 * 如果仍然超時,則強制退出. 
	 * 另对在shutdown时线程本身被调用中断做了处理. 
	 */ 
	public static void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout,
			TimeUnit timeUnit) {
		pool.shutdown(); // Disable new tasks from being submitted
		try { 
			// Wait a while for existing tasks to terminate 
			if (!pool.awaitTermination(shutdownTimeout, timeUnit)) {
				pool.shutdownNow(); // Cancel currently executing tasks
				// Wait a while for tasks to respond to being cancelled 
				if (!pool.awaitTermination(shutdownNowTimeout, timeUnit)) {
					System.err.println("Pool did not terminated");
				} 
			} 
		} catch (InterruptedException ie) {
			// (Re-)Cancel if current thread also interrupted 
			pool.shutdownNow();
			// Preserve interrupt status 
			Thread.currentThread().interrupt();

7
CodeRank
This code example shows how to use the following methods:shutdown
    @Override 
    public synchronized void stop(StopContext context) {
        final ExecutorService executorService = this.registrations;
        this.registrations = null;
        if(executorService != null) {
            executorService.shutdown();
        } 
    } 
 
    /** {@inheritDoc} */ 
    @Override 
    public synchronized ManagementChannelInitialization getValue() throws IllegalStateException, IllegalArgumentException { 
        return this;
    } 
 
    @Override 
    public ManagementChannelHandler startReceiving(final Channel channel) {
        final ManagementChannelHandler channelHandler = new ManagementChannelHandler(channel, executorService);
        final ServerToHostProtocolHandler registrationHandler = new ServerToHostProtocolHandler(serverInventory.getValue(), operationExecutor, domainController, channelHandler, registrations, expressionResolver);
        channelHandler.addHandlerFactory(new ManagementPongRequestHandler());
7
CodeRank

Related Java 8 ExecutorService Questions & Answers:

Other Java 8 ExecutorService Examples

25:	    FileCache fileCache;
26:	    private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
27:	    ExecutorService executorService; 
28:	 
29:	    public ImageLoader(Context context){

Read More

25:	    private Map&lt;ImageView, String&gt; imageViews=Collections.synchronizedMap(new WeakHashMap&lt;ImageView, String&gt;());
26:	    ExecutorService executorService; 
27:	
28:	    public ImageLoader(Context context){
29:	        fileCache=new FileCache(context);

Read More

See Code Examples for Java 8 ExecutorService Methods: