stop() 을 호출하여 스레드 실행 상태를 중지하려는 경우 스레드 메소드 Java의 클래스입니다. 이 메서드는 실행 중인 스레드의 실행을 중지하고 대기 중인 스레드 풀과 수집된 가비지에서 스레드를 제거합니다. 스레드는 메서드의 끝에 도달하면 자동으로 데드 상태로 이동합니다. 중지() 메소드가 더 이상 사용되지 않습니다. 스레드 안전성으로 인해 Java에서 문제.
구문
@Deprecated public final void stop()
예시
import static java.lang.Thread.currentThread;
public class ThreadStopTest {
public static void main(String args[]) throws InterruptedException {
UserThread userThread = new UserThread();
Thread thread = new Thread(userThread, "T1");
thread.start();
System.out.println(currentThread().getName() + " is stopping user thread");
userThread.stop();
Thread.sleep(2000);
System.out.println(currentThread().getName() + " is finished now");
}
}
class UserThread implements Runnable {
private volatile boolean exit = false;
public void run() {
while(!exit) {
System.out.println("The user thread is running");
}
System.out.println("The user thread is now stopped");
}
public void stop() {
exit = true;
}
} 출력
main is stopping user thread The user thread is running The user thread is now stopped main is finished now