To receive the results of the implementation of the thread pool by Callable and Future

In the Java thread execution, whether it is the direct successor Thread way, or ways to achieve Runnable interface, will not get the results returned to the thread of execution. If this thread error occurs during execution, the main thread will not be perceived. Even if the print log can not be thrown immediately. Afterwards view the log to discover there was bug. And then the code to point the problem occurred from a real problem might be very different. If there is a bug in the process of execution of the thread pool thrown in a timely manner, then this would be a good implementation. To solve this problem is to use the Callable interface that can return the thread to get results, to undertake by Future's get method. The following examples cumulative realized by a 1000 thread, to demonstrate the use of Callable and Future:

package com.hys.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

public class Test {

private static AtomicInteger num = new AtomicInteger();

public static void main(String[] args) throws InterruptedException, ExecutionException {
CountDownLatch latch = new CountDownLatch(1000);
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("increment-pool-%d").build();
ExecutorService poolexecutor = new ThreadPoolExecutor(1000, 1000, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
Future<String> submit = null;
for (int i = 0; i < 1000; i++) {
if (submit != null && submit.get() != null) {
latch.countDown();
continue;
}
submit = poolexecutor.submit(() -> {
try {
//这里模拟一个耗时很长的操作
num.getAndIncrement();
//int a = 1 / 0;
Thread.sleep(1);
return null;
The catch} (Exception E) {
return e.toString ();
} {the finally
latch.countDown ();
}
});
}
poolexecutor.shutdown (http://www.amjmh.com/v/);
// main thread wait for all sub-thread execution is completed before execution
latch.await ();
String errorMsg = submit.get ();
// if the child thread error during execution, then this thrown
if (errorMsg = null!) {
the throw a RuntimeException new new (errorMsg);
}
System.out.println (NUM);
}
}
If each thread no problems occur during the execution, the result returned is null. If the return result is not null, the code represents the thread of execution there is a problem, then the error message is returned. Note the above-described release code line 33, to simulate an arithmetic exception code is executed again, the following results can be obtained:

Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
at com.hys.test.Test.main(Test.java:49)
--------------------- 

Guess you like

Origin www.cnblogs.com/liyanyan665/p/11332649.html
Recommended