자바에서 비동기 다중 스레드 시간 초과로 인한 서비스 이상에 대해 간단히 말하다
그 중 한 라인이 시간을 초과할 때 이론적으로 다른 라인의 집행 결과에 영향을 주지 않아야 하지만 프로젝트에서 발생한 문제는 한 라인이 막히고 다른 라인이 돌아오는 인터페이스가 비어 있음을 나타낸다.사실은 매우 간단한 문제였지만, 처음 만났기 때문에, 그래도 약간의 시간을 생각했다.간단해, 바로 막힌 그 선 때문이야.
노드가 방출되지 않고 병발량이 많으면 노드 탱크의 수량이 가득 차서 다른 노드는 모두 대기 상태에 있다.
자신이 쓴 디버깅 코드를 첨부하여 문제가 생각나지 않을 때, 자신이 모의한 디버깅을 하고, 문제가 정해지지 않으면 나온다.
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();
}
}
이상은 여러분이 가져온 간단한 자바에서 비동기 다중 노드 시간 초과로 인한 서비스 이상 모든 내용입니다. 많은 응원 부탁드립니다~
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.