이것은 인터럽트를 사용하여 스레드를 종료/종료하고 Ctrl-c로 처리하는 두 번째 게시물입니다. 나는 그것을 이해하고 있는지 모르겠지만 여기에 최선의 시도가있다. 개념에 대한 명확한 설명이 필요하며 코드 예제를 제공하십시오.Threads Java Inturrupts
메인 클래스 Quitit과 다른 클래스 이 있습니다.입니다. 메인 클래스.
터미널 (Linux의 경우)를 통해 프로그램을로드 할 때 :
자바 -jar Quitit.jar
당신 Ctrl 키-C는 내가 그 말에 수정입니다 닫습니다 때 다음을 수행해야합니다.
Runtime.getRuntime() addShutdownHook()를
스레드가 종료 될 때 종료되도록 스레드.
- 어떻게 해결할 수 있습니까?
- 정상적으로 닫힐 수 있도록 메소드를 호출 할 수없는 이유는 무엇입니까?
Ctrl-C를 사용하여 종료하지 않고 Thread.Interrupt()를 통해이를 수행하려는 경우 아래 프로그램에서 올바르게 사용합니까?
- Thread.Join()이 계속 진행하기 전에 대상 스레드가 죽을 때까지 호출 스레드를 중단합니다.
- 어떻게 구현/호출합니까? Runtime.getRuntime(). addShutdownHook()에 Runnable 스레드를 구현하는 대신 스레드를 확장 하시겠습니까?
Quitit 클래스 :
public class Quitit extends Thread {
public Quitit(String name) {
try {
connect("sdfsd");
} catch (Exception ex) {
}
}
void connect(String portName) throws Exception {
Thread thh = new thething("blaghname");
Runtime.getRuntime().addShutdownHook(thh);
thh.start();
System.out.println("Thread Thh (thething) Started()");
while (true) {
try {
Thread.sleep(2000);
if (thh.isAlive()) {
System.out.println("Thread Thh (thething) isAlive");
if (thh.isInterrupted()) {
System.out.println("Thread Thh (thething) is Inturrupted Should be Shutting Down");
} else {
System.out.println("Thread Thh (thething) is Not Inturrupted");
thh.interrupt();
System.out.println("Thread Thh (thething) Inturrput Sent");
System.out.println("Thread Thh (thething) Joined()");
thh.join();
}
} else {
System.out.println("Thread Thh (thething) isDead");
System.out.println("Main Thread:: can now end After Sleep off 2 seconds");
Thread.sleep(2000);
System.out.println("MMain Thread:: Sleep Ended Calling Break");
break;
}
} catch (InterruptedException xa) {
System.out.println("Main Thread:: ending due to InterruptException second Break called");
break;
}
}
System.out.println("Main Thread:: Outside While(true) via Break call");
}
public static void main(String[] args) {
try {
Thread oop = new Quitit("");
Runtime.getRuntime().addShutdownHook(oop);
oop.start();
} catch (Exception ezx) {
System.out.println("Main Thread:: Not Expected Exception");
}
}
}
TheThing 클래스 :
public class thething extends Thread {
thething(String name) {
super(name);
}
@Override
public void run() {
while (true) {
try {
System.out.println("thething class:: Inside while(true) Loop, now sleeping for 2 seconds");
Thread.sleep(2000);
} catch (InterruptedException e) {
try {
System.out.println("thething class:: has been Inturrupted now sleeping for 2 seconds!!");
Thread.sleep(2000);
break; // Will Quit the While(true) Loop
} catch (InterruptedException ex) {
System.out.println("thething class:: Second InterruptedException called !!");
}
}
}
System.out.println("thething class:: Outside while(true) and now thread is dying");
}
}
출력 ::
run:
Thread Thh (thething) Started()
thething class:: Inside while(true) Loop, now sleeping for 2 seconds
Thread Thh (thething) isAlive
Thread Thh (thething) is Not Inturrupted
Thread Thh (thething) Inturrput Sent
Thread Thh (thething) Joined()
thething class:: has been Inturrupted now sleeping for 2 seconds!!
thething class:: Outside while(true) and now thread is dying
Thread Thh (thething) isDead
Main Thread:: can now end After Sleep off 2 seconds
MMain Thread:: Sleep Ended Calling Break
Main Thread:: Outside While(true) via Break call
FINISHED - BUILD SUCCESSFUL (total time: 8 seconds)
'Quitit' 생성자에서'super()'를 호출해야합니다. – artbristol
Ok 이것은 쓰레드를 죽이는 올바른 방법이 interrupt()가 아니기 때문에 다시 혼란 스럽습니다. while (true)/while (false) 루프를 제어하고 스레드가 종료되도록하는 부울 플래그를 사용하여 스레드를 종료해야합니다. 인터럽트는 프로그램을 무기한 보관할 수있는 제한 시간없이 Read()를 할 수있는 직렬 포트 read() (IO Reads)와 같은 대기 상태가있는 경우에만 사용해야합니다. 투명도가 정말 실종 된 것처럼 보입니까, 아니면 나입니까? – DevilCode
@DevilCode : 인터럽트는 스레드에게 중지를 요청하는 올바른 방법입니다. 추가 플래그 필요 없음 : 인터럽트 상태가이를 수행 할 수 있습니다. –