많은 지사에서 해당 스레드를 지점 코드로 호출하는 스레드 (Runnable 구현)가 있습니다. 브랜치 코드로 스레드의 이름을 설정했습니다. 문제는 ...많은 스레드 및 예외 스레드 예외 실행
- 실행중인 스레드에서 예외가 발생했을 때이를 멈출 수 없습니다. "ExceptionInInitializerError"또는 "OutOfMemoryError : Java heap space"라는 이름의 다른 스레드를 만들려고하면
- "OutOfMemoryError : Java 힙 공간"예외가 발생합니다. 한 번에 2 개 이상의 스레드가 실행 중일 때 예외가 발생합니다.
public MyRunnerClass {
//This method called from many branch with their branch Code
public void executeBranchProcess(String branchCode){
Thread t = new Thread(new Exporter(branchCode);
t.setName(branchCode);
t.start();
}
}
스레드 클래스는 여기
public class Exporter implements Runnable{
private String branchCode;
public Exporter(String branchCode){
this.branchCode = branchCode;
}
@Override
public void run() {
try {
exportCorner();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void exportCorner() throws InterruptedException{
try{
//Some Process
}catch(Exception e){
// I want to close running thread here
// I am using closeThread(this.branchCode), but not working
}
}
static void closeThread(String branchCode) throws InterruptedException {
Thread thread = null;
for (Thread t : Thread.getAllStackTraces().keySet()) {
if (t.getName().equals(branchCode))
thread = t;
}
if (thread != null) {
thread.interrupt();
thread.join();
}
}
}
나는 당신이 무엇을 요구하고 있는지 전혀 모른다. 스레드의 run 메소드 내에서 예외가 발생하면, 단순히 예외 처리 대신 무언가를 처리하는 끝없는 루프를 구현하지 않는 한 간단히 종료됩니다. 다른 말로하면 : 스레드가 예외에 부딪히면 코드가 해당 메서드에서 "반환"됩니다 (예를 들어 예외를 로깅 한 후). – GhostCat
가능한 [runnable 인터페이스를 구현하여 만든 스레드를 중지하는 방법?] (http://stackoverflow.com/questions/10630737/how-to-stop-a-thread-created-by-implementing-runnable-interface) –
'ExecutorService'를 사용하고 작업을 제출할 때 반환되는'Future'를 취소하십시오. 또한 Runnable 대신에'Callable'을 사용할 수 있습니다.'call()'메서드는 명시 적으로 처리하는 대신 검사 된 예외를 던질 수 있도록합니다. –