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

What are the scenarios that require thread interruptions in java

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

Share

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

This article mainly introduces the scenarios that need thread interruption in java. It is very detailed and has a certain reference value. Friends who are interested must finish it!

Scenarios where thread interruptions are required

Many threads run in an endless loop, for example, in the producer / consumer model, the consumer subject is an endless loop that constantly accepts tasks from the queue and executes them, and when stopping the program, we need an "elegant" way to shut down the thread.

In some user-initiated tasks, the thread is started by the user, such as manually starting a batch task, and the user may want to cancel the task during task execution.

In some scenarios, such as querying a result from a third-party server, we want to get the result within a limited time, and if we don't get it, we want to cancel the task.

Sometimes, we will start multiple threads to do the same thing, such as grabbing train tickets, we may ask multiple friends to help buy train tickets from multiple channels, as long as there is one channel, we will notify the cancellation of other channels.

The thread method involved (Thread object method)

Public boolean isInterrupted () determines whether the thread interrupt flag bit is true

Public void interrupt () sets the thread interrupt flag to true, but it may not be successful for different states of the thread.

Public static boolean interrupted () returns the thread interrupt flag bit and clears it.

Thread's response to interrupts

There are several situations:

RUNNABLE state-the thread called the start () method, waiting for the system to be scheduled or running

In this case, only the interrupt flag bit is set.

WAITING/TIMED_WAITING- wait statu

Enter the waiting state when calling the following methods, including the following methods:

WATING: the wait method of the lock resource is called or the join method is called.

TIMED_WAITING:wait (long timeout), sleep (long millis), join (long millis). The difference between wait and sleep: whether to release the lock.

An InterruptedException exception is thrown and the thread interrupt flag bit is cleared, which is usually handled by the superior. If you want the thread to interrupt, perform the cleaning work or reset the thread flag bit in the catch.

The BLOCKED- thread is in a lock waiting queue, trying to enter the synchronization block

Only flag bits are set.

The NEW/TERMINATED- thread has finished or has not yet called the start () method

It won't have any effect.

Be careful

This is not to say that when the interrupt () method is called, the thread is terminated. Those who need to implement the thread are implemented in code, as follows:

While (! Thread.currentThread (). IsInterrupted ()) {/ / cleanup logic} above is all the content of the article "what are the scenarios in java that require thread interruptions?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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

Internet Technology

Wechat

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

12
Report