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 understand the problem of thread interruption

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

Share

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

This article mainly talks about "how to understand the problem of thread interruption". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the problem of thread interruption.

There is essentially only one way to implement a thread

Why not force it to stop?

For Java, the most correct way to stop a thread is to use interrupt. But interrupt only serves to notify the stopped thread. For the stopped thread, it has full autonomy, it can choose to stop immediately, can choose to stop after a period of time, or can choose not to stop at all.

Why doesn't Java provide the ability to force a thread to stop? In fact, Java wants programs to be able to notify each other and cooperate with each other to manage threads, because if you don't know what each other is doing, rashly forcing threads to stop can cause some safety problems.

For example, when a thread is writing a file and receives a termination signal, it needs to choose whether to stop immediately or to stop writing the whole file successfully according to its own business judgment. If you choose to stop immediately, the data may be incomplete, and neither the initiator of the interrupt command nor the receiver wants the data to have a problem.

How to stop a thread with interrupt

While (! Thread.currentThread (). IsInterrupted () & & more work to do) {do more work}

Once we call a thread's interrupt (), the thread's interrupt flag bit is set to true. Every thread has such a tag bit, which should be checked periodically when the thread executes. If the flag bit is set to true, it means that a program wants to terminate the thread.

Going back to the source code, you can see that in the while loop body judgment statement, first pass the

Thread.currentThread () .isInterrupt ()

Determine whether the thread is interrupted, and then check to see if there is still work to be done. The logic indicates that the following work will be performed only if the two judgment conditions are met at the same time.

Public class StopThread implements Runnable {@ Override public void run () {int count = 0; while (! Thread.currentThread (). IsInterrupted () & & count

< 1000) { System.out.println("count = " + count++); } } public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(new StopThread()); thread.start(); Thread.sleep(5); thread.interrupt(); } } 在 StopThread 类的 run() 方法中,首先判断线程是否被中断,然后判断 count 值是否小于 1000。 这个线程的工作内容很简单,就是打印 0~999 的数字,每打印一个数字 count 值加 1,可以看到,线程会在每次循环开始之前,检查是否被中断了。接下来在 main 函数中会启动该线程,然后休眠 5 毫秒后立刻中断线程,该线程会检测到中断信号,于是在还没打印完1000个数的时候就会停下来,这种就属于通过 interrupt 正确停止线程的情况。 sleep 期间能否感受到中断 先说结论,可以。 public class StopDuringSleep { public static void main(String[] args) throws InterruptedException { Runnable runnable = () ->

{int num = 0; try {while (! Thread.currentThread (). IsInterrupted () & & num

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