当前位置:网站首页>Fundamentals of concurrent programming (III)
Fundamentals of concurrent programming (III)
2022-07-03 12:11:00 【zwanying】
The basis of concurrent programming ( 3、 ... and )
The guardian thread User threads
The guardian thread :daemon Threads , Background process that realizes general functions , Not necessary . for example Garbage collection
User threads : for example ,main Method , After the user process completes execution, the daemon will automatically destroy ,JVM sign out .
Use
Set a thread as a guardian thread , Need to be in start() Before the method thread executes .
t1.setDaemon(true);
After setting to daemon thread , Even if the execution is not completed , After the main thread finishes executing , Automatically destroy ,JVM I'll quit .
scene
It can be turned off at any time , There will be no adverse consequences . Generally, it serves user threads , Low priority , Garbage collection , Heartbeat monitoring , Temporary data cleaning, etc .
ThreadLocal
Thread local variable . When multithreading operates variables , The operation is local copy , To avoid thread safety problems .
Use
get() set() Method , Setting multiple values will be overwritten .
remove() Empty
ThreadLocal<String> threadLocal = new ThreadLocal<>();
threadLocal.set("hello");
threadLocal.set("work");
System.out.println(threadLocal.get());
threadLocal.remove();
System.out.println(threadLocal.get());
Multithreading does not interfere with each other
public class Test {
static ThreadLocal<String> threadLocal = new ThreadLocal<>();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable(){
@Override
public void run(){
threadLocal.set("t1");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(threadLocal.get());
}
});
Thread t2 = new Thread(new Runnable(){
@Override
public void run(){
threadLocal.set("t2");
System.out.println(threadLocal.get());
threadLocal.remove();
}
});
t1.start();
t2.start();
}
}
边栏推荐
- vulnhub之presidential
- [learning notes] DP status and transfer
- Ripper of vulnhub
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- vulnhub之Ripper
- Dart: view the dill compiled code file
- [combinatorics] permutation and combination (example of permutation and combination)
- Ripper of vulnhub
- Qt OpenGL相机的使用
- OpenGL index cache object EBO and lineweight mode
猜你喜欢

Integer string int mutual conversion

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

Is BigDecimal safe to calculate the amount? Look at these five pits~~

OpenGL 索引缓存对象EBO和线宽模式

STL Tutorial 9 deep copy and shallow copy of container elements

Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements

Duplicate numbers in the array of sword finger offer 03

Vulnhub's Nagini

OpenGL draws colored triangles

网络通讯之Socket-Tcp(一)
随机推荐
vulnhub之narak
【附下载】密码获取工具LaZagne安装及使用
Quantitative calculation research
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
vulnhub之pyexp
Vulnhub's Tomato (tomato)
php 获取文件夹下面的文件列表和文件夹列表
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Redis
OpenGL draws colored triangles
(数据库提权——Redis)Redis未授权访问漏洞总结
Test classification in openstack
Go language to realize static server
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
[official MySQL document] deadlock
Dart: About zone
Differences between MySQL Union and union all
vulnhub之momentum
temp
Pragma pack syntax and usage