2012-06-23 7 views
1

실행자의 스레드를 방해하는 올바른 방법은 무엇입니까? 와실행자 스레드를 인터럽트하는 방법

public void run() { 
    while(!(Thread.currentThread().isInterrupted()){ 
     System.out.println("work " + Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted()); 
    } 
} 

그리고 메인 클래스 : : 방법 이름 작업자 Thread 클래스 : 나는이있어

ExecutorService executorService = Executors.newFixedThreadPool(threadCount); 
Worker worker = new Worker();     
executorService.execute(worker); 

내가 worker.interrupt(); 또는 executorService.shutdownNow(); 전화를 시도를하지만 내 스레드에와 isInterrupted (간다)가 거짓입니다.

+0

확인이 밖으로있는 가능한 복제본 일 수 있습니다. http://stackoverflow.com/questions/7142665/why-does-thread-isinterrupted-always-return-false – GETah

답변

1

관련 코드를 모두 게시 할 수 있습니까? 귀하가 제공 한 정보를 토대로 귀하가 설명하는 행동을 재현 할 수 없습니다. 예상대로 작동하는 SSCCE 아래 참조 - 출력 :

작업 풀-1 스레드 1 : 거짓
작업 풀-1 스레드 1 : 거짓
작업 풀-1 스레드 1 :
거짓 ....
스레드가 중단 된

코드 :

public class Test { 

    public static void main(String[] args) throws InterruptedException { 
     ExecutorService executorService = Executors.newFixedThreadPool(1); 
     Worker worker = new Worker(); 
     executorService.execute(worker); 
     executorService.shutdownNow(); 
    } 

    public static class Worker extends Thread { 

     public void run() { 
      while (!Thread.currentThread().isInterrupted()) { 
       System.out.println("work " + Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted()); 
      } 
      System.out.println("Thread has been interrupted"); 
     } 
    } 
} 
+0

해답을 가져 주셔서 감사합니다. 귀하의 코드는 잘 작동합니다. 차이점은 실행 메소드의 Worker 클래스에서 JMS로 작업한다는 것입니다. 'messageService.sendMessage (message);'한 줄만 주석 처리하면된다. 주석 처리를한다면, 항상'work pool-1-thread-1 : false'가 아니라면 스레드는 멈춘다. 그리고 난 그냥 내 자신의 플래그 isInterrupted() 대신 스레드를 멈출 수 – user1406196

+0

아마도 그 방법은 블록 때문에 ... – assylias

관련 문제