Concurrency basis (ix) termination and interruption of Java threads

Source: Internet
Author: User
Tags volatile

1, simple to understand: Why do not agree with the use of Thread.stop, Thread.Suspend and Thread.Resume?

?? The suspend, resume, and stop methods are used to suspend, resume, and terminate the threads respectively. The reason for this is not recommended: Because of the side effects of these three methods, such as the Suspend () method, the thread will always take up resource sleep until resume () is called, before it can be run. This can easily cause deadlocks. Similarly, the Stop () method does not guarantee that a thread's resources are properly freed when it ends a thread, and therefore causes the program to work in an indeterminate state.
?? The Suspend/resume operation of a thread can be replaced by a wait/notification mechanism. But the termination of the thread will be implemented by the user.
For more information, refer to: http://bjzhkuang.iteye.com/blog/1748396

2. How to gracefully and safely terminate a thread

?? Terminating a thread is essentially a process of communication interaction between threads. Therefore, the way of communication between threads: Shared variables, streams, interrupts can be used to implement the mechanism of terminating the thread. The most common way is "interruption";
The following example is: In addition to interrupts, a Boolean variable can be used to control whether the thread needs to be terminated;

 Public classthreadtest{ Public Static void Main(string[] args)throwsinterruptedexception {MyTask TaskOne =New MyTask(); Thread Threadone =NewThread (TaskOne,"Threadone"); Threadone.Start(); Timeunit.MILLISECONDS.Sleep( -); Threadone.Interrupt(); MyTask Tasktwo =New MyTask(); Thread Threadtwo =NewThread (Tasktwo,"Threadtwo"); Threadtwo.Start(); Timeunit.MILLISECONDS.Sleep( -); Tasktwo.Cancel();}Private Static classMyTaskImplementsrunnable{//To add the volatile modifier to ensure the visibility of the memory, update the value of the variable in time    Private volatile BooleanOn =true;Private LongI@Override     Public void Run() {//Call Cancel, or interrupt to terminate the thread         while(On &&!) Thread.CurrentThread().isinterrupted()) {i++; } System. out.println("Threads"+thread.CurrentThread().GetName()+"Count i ="+i); } Public void Cancel() {on =false; }   }}

Operation Result:

Thread Threadone Count i = 239418320
Thread Threadtwo Count i = 235374425

Concurrency basis (ix) termination and interruption of Java threads

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.