자바에서 비동기 다중 스레드 시간 초과로 인한 서비스 이상에 대해 간단히 말하다

프로젝트에서 병발량이 많을 때의 성능 안정성을 높이기 위해 스레드 탱크에 자주 사용하여 다중 스레드 비동기 조작을 한다. 다중 스레드는 2가지가 있는데 하나는 runnable 인터페이스를 실현하는 것이다. 이런 것은 되돌아오는 값이 없고 하나는 Callable 인터페이스를 실현하는 것이다. 이런 것은 되돌아오는 값이 있다.
그 중 한 라인이 시간을 초과할 때 이론적으로 다른 라인의 집행 결과에 영향을 주지 않아야 하지만 프로젝트에서 발생한 문제는 한 라인이 막히고 다른 라인이 돌아오는 인터페이스가 비어 있음을 나타낸다.사실은 매우 간단한 문제였지만, 처음 만났기 때문에, 그래도 약간의 시간을 생각했다.간단해, 바로 막힌 그 선 때문이야.
노드가 방출되지 않고 병발량이 많으면 노드 탱크의 수량이 가득 차서 다른 노드는 모두 대기 상태에 있다.
자신이 쓴 디버깅 코드를 첨부하여 문제가 생각나지 않을 때, 자신이 모의한 디버깅을 하고, 문제가 정해지지 않으면 나온다.

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();
    } 
}
이상은 여러분이 가져온 간단한 자바에서 비동기 다중 노드 시간 초과로 인한 서비스 이상 모든 내용입니다. 많은 응원 부탁드립니다~

좋은 웹페이지 즐겨찾기