Service exceptions due to asynchronous multithreaded timeouts in Java

Source: Internet
Author: User

In the project in order to improve the performance stability of large concurrency, often use the thread pool to do multi-threaded asynchronous operations, there are 2 of multithreading, one is to implement the Runnable interface, this has no return value, one is to implement the callable interface, this has a return value.

When one of the lines is blocks until those, it should theoretically not affect the execution of the other threads, but the problem in the project indicates that one thread is blocked and the other threads return an empty interface. It was a very simple question, but I thought about it for the first time. It is simply because the blocked thread is not released, the amount of concurrency is large, and the number of thread pools is full, so the other threads are in a wait state.

Attach a section of your own debugging code, when you want to do not have problems, their own simulation of writing, maybe the problem comes out.

Importjava.util.concurrent.Callable;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future;ImportJava.util.concurrent.TimeUnit;Importjava.util.concurrent.TimeoutException; Public classfuturetest{ Public Static voidMain (string[] args)throwsinterruptedexception, Executionexception, timeoutexception {FinalExecutorservice exec = Executors.newfixedthreadpool (1); Callable<String> call =NewCallable<string>() {             PublicString Call ()throwsinterruptedexception {//start a time-consuming operationThread.Sleep (1000 * 2); return"1 Thread execution completed.";        }        }; Callable<String> call2 =NewCallable<string>() {             PublicString Call ()throwsException {//start a time-consuming operation//Thread.Sleep (+ 5);                return"2 thread execution completed.";                }        }; Callable<String> Call3 =NewCallable<string>() {             PublicString Call ()throwsException {//start a time-consuming operation//Thread.Sleep (+ 5);                return"3 Thread execution 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 (timeunit.milliseconds);//task processing time-out is set to}//1 seconds            Catch(Exception e) {System.out.println ("Processing timed out ....");            E.printstacktrace (); }                        Try{obj3= Future3.get (timeunit.milliseconds);//task processing time-out is set to}//1 seconds               Catch(Exception e) {System.out.println ("Processing timed out ....");               E.printstacktrace (); }                        Try{obj2= Future2.get (3000, timeunit.milliseconds);} Catch(Exception e) {System.out.println ("Processing timed out ....");            E.printstacktrace (); } System.out.println ("3 Task successfully returned:" +obj3); System.out.println ("2 task successfully returned:" +obj2); System.out.println ("1 task successfully returned:" +obj);        Exec.shutdown (); }  }

Service exceptions due to asynchronous multithreaded timeouts in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.