Multithreading | Problem multiple Thread thread.stop () The reason for being discarded:
When the Thread.stop () method is invoked, the thread releases all of the resources it has previously controlled, and after forcing the stop after the threads have not performed properly, the resources may be in an inconsistent state, and after the resources in the inconsistent state are used by other threads, There may be some unexpected mistakes.
To achieve the solution of the time difference event:
Set a state variable in the main thread, sleep () a fixed period of time when the response thread executes, then check this state of the main thread, and if the state is different, perform a different operation or stop execution.
The callback mechanism enables the response thread to view the main thread variable, 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 ();
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service