当前位置:网站首页>I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
2022-06-26 13:30:00 【Tom bomb architecture】
You can scan the QR code at the bottom of the article to get the full version of the interview questions !
One bit 5 In, my little friend went to a certain East for an interview and was given a concurrent programming interview question Pass 了 , say ” How to interrupt a running thread ?, A lot of work has been done on this issue 2 We all know , It's a pity .
today , Let me share with you my answer .
1、 What is thread ?
Before I answer that question , Let's review what threads ?
Thread, Thread is the smallest unit of operation scheduling . therefore , Thread is a system level concept .

And in the Java The thread implemented inside , The final execution and scheduling are determined by the operating system ,JVM It's just a layer of packaging for threads at the operating system level .
So we are Java It calls start() Method starts a thread , Just tell the operating system that this thread can be executed , But finally to CPU To execute , It is determined by the scheduling algorithm of the operating system .
2、 How to operate how to safely interrupt a running thread ?
In theory , To be in Java Level to interrupt a running thread , Only like Linux Inside kill The command ends the process in the same way , Mandatory termination .
Java Thread Of API Although it provides a stop() Method , Threads can be forcibly terminated , But this way is not safe , Because it is possible that the task of the thread has not been completed , Sudden interruptions can lead to incorrect results .
To safely interrupt a running thread , Only one hook can be buried inside the thread , External procedures Use this hook to trigger the interrupt command of the thread .
therefore , stay Java Thread There is a interrupt() Method , This method should cooperate with isInterrupted() Method to use , You can safely interrupt the thread .

Look at this code :
Runnable runnable = new Runnable(){
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()){
System.out.println(" The thread was interrupted ");
return ;
} else {
System.out.println(" The thread is not interrupted ");
}
}
}
};
Thread t = new Thread(runnable);
t.start();
Thread.sleep(500);
t.interrupt();
System.out.println(" Thread interrupted , Here's the program ");This implementation is not a forced interrupt , Instead, it tells the running thread , You can stop . When to actually interrupt , Depends on the running thread , therefore , It can ensure the security of the running results of threads .
This problem , A lot of work 5 Little friends over years don't necessarily know . although CRUD Proficient , however , It is often outside of work that others are left behind 8 Hours .
Pay attention to WeChat public number 『 Tom Bomb architecture 』 reply “666” Available 200 page pdf Format document ( Complimentary gift 10 Internal interview materials of Wanzi factory ). Thank you for your support !

You can also scan the QR code below +\/ Get the full version free of charge !
↓ ↓ ↓ ↓ ↓
边栏推荐
- HDU 3555 Bomb
- Arcpy——InsertLayer()函数的使用:掺入图层到地图文档里
- Mode pont
- [node.js] MySQL module
- Aesthetic experience (episode 238) Luo Guozheng
- Beifu twincat3 can read and write CSV and txt files
- Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
- Script - crawl the customized storage path of the cartoon and download it to the local
- Lamp compilation and installation
- KITTI Detection dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
猜你喜欢

Here document interaction free and expect automatic interaction

Beifu PLC realizes data power-off maintenance based on cx5130

ES基於Snapshot(快照)的數據備份和還原

mysql讲解(一)

【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
![[how to connect the network] Chapter 2 (next): receiving a network packet](/img/f5/33e1fd8636fcc80430b3860d069866.png)
[how to connect the network] Chapter 2 (next): receiving a network packet

2. Introduction to parallel interface, protocol and related chips (8080, 8060)

Echart stack histogram: add white spacing effect setting between color blocks

MongoDB系列之Window环境部署配置

First pass! Baidu AI Cloud Xiling platform has obtained the authoritative certification of digital human ability evaluation from the Institute of information technology
随机推荐
Arcpy - - utilisation de la fonction insertlayer (): ajout de calques dans un document de carte
三维向量的夹角
12 SQL optimization schemes summarized by old drivers (very practical)
ES中索引别名(alias)的到底有什么用
Adapter mode
code force Party Lemonade
MediaPipe手势(Hands)
[node.js] MySQL module
MySQL explanation (I)
古瑞瓦特冲刺港交所上市:创下“多个第一”,获IDG资本9亿元投资
MongoDB系列之Window环境部署配置
Decorator
HDU 5860
ES6:Map
Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value
7-2 the cubic root of a number
Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program
LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏