Home > Java > javaTutorial > body text

A brief discussion on service exceptions caused by asynchronous multi-thread timeout in Java

高洛峰
Release: 2017-01-05 14:50:39
Original
1319 people have browsed it

In order to improve the performance stability of large concurrency in projects, thread pools are often used to perform multi-threaded asynchronous operations. There are two types of multi-threads. One is to implement the runnable interface, which has no return value, and the other is to implement the runnable interface. The first is to implement the Callable interface, which has a return value.

When one of the threads times out, theoretically it should not affect the execution results of other threads. However, problems that occurred in the project showed that one thread was blocked and the interfaces returned by other threads were empty. It's actually a very simple question, but since it was the first time I encountered it, I still thought about it for some time. It's very simple, because the blocked thread

has not been released. If the concurrency is large, the number of thread pools will be full, so other threads are in a waiting state.

Attached is a piece of debugging code that I wrote. When I can’t figure out the problem, I can simulate it and write it myself. 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
      {
        // 开始执行耗时操作
          Thread.sleep(1000 * 2); 
        return "1线程执行完成.";
      }
    };
 
    Callable<String> call2 = new Callable<String>() {
      public String call() throws Exception
      {
        // 开始执行耗时操作
        // Thread.sleep(1000 * 5);
        return "2线程执行完成.";
      }
    };
     
    Callable<String> call3 = new Callable<String>() {
      public String call() throws Exception
      {
        // 开始执行耗时操作
        // Thread.sleep(1000 * 5);
        return "3线程执行完成.";
      }
    };
 
    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); // 任务处理超时时间设为
      }// 1 秒
      catch(Exception e){
        System.out.println("处理超时啦....");
        e.printStackTrace();
      }
       
      try{
        obj3 = future3.get(3000, TimeUnit.MILLISECONDS); // 任务处理超时时间设为
        }// 1 秒
        catch(Exception e){
          System.out.println("处理超时啦....");
          e.printStackTrace();
        }
       
      try{
       obj2 = future2.get(3000, TimeUnit.MILLISECONDS);}
      catch(Exception e){
        System.out.println("处理超时啦....");
        e.printStackTrace();
      }
      System.out.println("3任务成功返回:" + obj3);
      System.out.println("2任务成功返回:" + obj2);
      System.out.println("1任务成功返回:" + obj);
      exec.shutdown();
    } 
}
Copy after login

The above is the brief discussion brought by the editor about the service exception caused by asynchronous multi-thread timeout in Java. I hope everyone will support the PHP Chinese website~

More brief discussion Please pay attention to the PHP Chinese website for related articles on service exceptions caused by asynchronous multi-thread timeout in Java!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template