Java advanced multi-thread programming-thread stop

Source: Internet
Author: User
Tags thread stop
Multithreading is an advantage of Java. Program Developers can easily develop multi-threaded programs. For better performance.
Regarding the concept of multithreading and general multi-threaded programming, such as how and why the runnable interface is implemented, and why stop () is deprecated, for details, refer to the multi-threaded programming basics of matrix or Sun's Java documentation.
For multithreaded programming, there are several points to mention:
1. Since stop () is not recommended, how do we stop a thread? Kill directly? Here, I will summarize a more general and stable method:

Class threadtest extend thread {
// Skip some code ..
Boolean runflag = true;
Public synchronized void stopthread ()
{
Runflag = false;
}

Public synchronized Boolean getrunflag ()
{
Return runflag;
}
Public void run (){
Runflag = true;
Try {
While (getrunflag ()){
Code1;
Code2;
// Put your code here
}
}
}
Catch (ioexception e ){
E. printstacktrace ();
}
System. Out. println (this. getclass (). getname () + "STOPPED ");
}
// Skip some code ..
}
In this way, you only need to call stopthread () whenever you need to stop the thread.

 

Note the following two points:

1) We used a synchronization method getrunflag () to get the current state. Why is this method used instead of using while (runflag) directly?
This is because there is a public Object Storage zone in the Java multi-threaded model, but each object has its own private backup. When a thread changes its status, JVM does not guarantee that the variables changed by this thread are updated to the state of the public Object Storage zone in real time, which may (unlikely) cause problems.
Therefore, we recommend that you have good design habits and use the synchronization method to obtain the current runflag value.

2) Another point is that if network congestion occurs (in the while loop), even if the runflag status changes to false, because the program is blocked, the thread will never stop using this method.
for example, in the preceding program, if code1 is a network program and code1 is blocked, the blocking means that the requested resources are not obtained.
, wait for an indefinite period. At this time, the change of the runflag status does not affect the while loop, and the thread will not be stopped.
I have participated in multiple Java programs involving obtaining network resources and often encounter thread problems caused by network congestion.
if your program may involve network congestion or a message may be blocked. Do not stop the thread in this way. For more information, see the author's Article : Advanced multithreaded programming (2)-monitoring and timeout issues in multithreading.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.