Thread.Stop () is discarded: When calling the thread.stop () method, the thread will release all resources that previously controlled, and after the thread is forced to force STOP, these resources may be In an inconsistent state, and these resources in inconsistent states may have some unexpected errors after being used by other threads.
Solution to time difference events:
Set a status variable in the main thread, when the response thread is executed, first SLEEP () a fixed time period, then check this state of the main thread, if this status is different, perform different operations, or stop execution.
You can achieve the response thread to the main thread variable by the callback mechanism, for example:
Test.java
Public class test {
Private Volatile Threadtest Test;
Public boolean flag = true;
Public test () {
Test = new threadtest (this);
}
Public static void main (String [] args) {
Test test = new test ();
Test.test.start ();
Test.flag = false;
Test = NULL;
}
}
Threadtest.java
Public class threadtest extends thread {
Test test;
Public Threadtest (Test Test) {
THIS.TEST = TEST;
}
Public void run () {
Try {
Sleep (1000);
IF (Test.Flag)
System.out.println ("I'VE WAKEN UP!");
} catch (exception e) {
E.PrintStackTrace ();
}
}
}