In order to improve the performance stability of large concurrency volume in the project, thread pools are often used to perform multi-thread asynchronous operations. There are two types of multi-threading. One is to implement the runnable interface, which has no return value, and the other is to implement the Callable interface, which has a return value.
When one of the threads timed out, it should not theoretically affect the execution results of other threads, but the problems that arise in the project indicate that one thread is blocked and the interfaces returned by the other threads are empty. Actually, it is a very simple question, but because I encountered it for the first time, I still thought about it for a while. It's very simple, it's because of the blocking line
The process is not released, and once the concurrency amount is large, the number of thread pools will be full, so other threads are in a waiting state.
Attached is a debugging code I wrote myself. When I can’t think of a problem, I simulated it and maybe the problem will come out.
import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;public class FutureTest{ public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { final ExecutorService exec = Executors.newFixedThreadPool(1); Callable<String> call = new Callable<String>() { public String call() throws InterruptedException { // Start executing time-consuming operation Thread.sleep(1000 * 2); return "1 thread execution completed."; } }; Callable<String> call2 = new Callable<String>() { public String call() throws Exception { // Start executing time-consuming operation // Thread.sleep(1000 * 5); return "2 thread execution is completed."; } }; Callable<String> call3 = new Callable<String>() { public String call() throws Exception { // Start executing time-consuming operation // Thread.sleep(1000 * 5); return "3 thread execution is completed."; } }; Future<String> future = exec.submit(call); Future<String> future3 = exec.submit(call3); Future<String> future2 = exec.submit(call2); String obj=""; String obj2 =""; String obj3 =""; try{ obj = future.get(500, TimeUnit.MILLISECONDS); // Set the task processing timeout to }// 1 second catch(Exception e){ System.out.println("Processing timeout...."); e.printStackTrace(); } try{ obj3 = future3.get(3000, TimeUnit.MILLISECONDS); // Set the task processing timeout to }// 1 second catch(Exception e){ System.out.println("Processing timeout......"); e.printStackTrace(); } try{ obj2 = future2.get(3000, TimeUnit.MILLISECONDS);} catch(Exception e){ System.out.println("Processing timeout...."); e.printStackTrace(); } System.out.println("3 task returns successfully:" + obj3); System.out.println("2 task returns successfully:" + obj2); System.out.println("1 task returns successfully:" + obj); exec.shutdown(); } }The above is the brief discussion of service exceptions caused by asynchronous multi-threading timeout in Java. I hope everyone will support Wulin.com more~