Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the two-phase termination thread of java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "java two-phase termination thread how to achieve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "java two-phase termination thread how to achieve" it!

How to shut down a thread gracefully?

How to gracefully close thread T2 in a thread T1 (that is, give T2 a chance to release the resources it holds)?

1. Wrong practice

Stop the thread using the stop () method:

The stop () method actually kills the thread, and if the thread holds the lock at this point, other threads will never be able to acquire the lock.

Stop the thread using the System.exit () method:

Will make the whole process exit.

two。 Correct approach

Train of thought:

Code implementation:

Public class Test {public static void main (String [] args) throws InterruptedException {TwoPhaseTermination twoPhaseTermination = new TwoPhaseTermination (); twoPhaseTermination.start (); Thread.sleep (3000); twoPhaseTermination.stop ();} class TwoPhaseTermination {/ / Monitoring thread private Thread monitorThread; public void start () {monitorThread = new Thread (()-> {Thread current = Thread.currentThread () While (true) {if (current.isInterrupted ()) {System.out.println ("Thread shuts down..."); break;} try {Thread.sleep (1000) / / Phase 1 System.out.println ("Monitoring thread is working") / / Phase 2 / / if interrupted at stage 2, the thread's isInterrupted flag bit is true, the thread will catch the signal and close the thread / / if interrupted at stage 1, it will enter the catch statement block, and the isInterrupted flag bit will be empty, unable to close the thread} catch (InterruptedException e) {e.printStackTrace () / / need to reset the isInterrupted flag bit to true monitorThread.interrupt ();}); / / start the thread monitorThread.start ();} public void stop () {/ / set the isInterrupted flag bit true monitorThread.interrupt ();}}

Running result:

Shut down the thread in two phases:

II. Main points

Why do you need to re-execute monitorThread.interrupt () in the catch code block? Because the Thread.sleep () execution is interrupted, the isInterrupted flag bit is cleared, and the next time you enter the while loop, the interrupt is ignored and the thread continues to run.

Demonstrate the result of commenting out monitorThread.interrupt ():

As you can see, the isInterrupted signal this time is ignored and the thread continues to run.

Thank you for your reading, the above is the "java two-phase termination thread how to achieve" content, after the study of this article, I believe you on the java two-phase termination thread how to achieve this problem has a deeper understanding, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report