当前位置:网站首页>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();
}
}
边栏推荐
- (数据库提权——Redis)Redis未授权访问漏洞总结
- Concurrent programming - singleton
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- AOSP ~ NTP (Network Time Protocol)
- OPenGL 基本知识(根据自己理解整理)
- (構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
- vulnhub之cereal
- STL Tutorial 9 deep copy and shallow copy of container elements
- Colleagues wrote a responsibility chain model, with countless bugs
- Dart: About zone
猜你喜欢

Vulnhub's presidential

Quantitative calculation research

Vulnhub's Tomato (tomato)

AOSP ~ NTP (Network Time Protocol)

Integer string int mutual conversion

4000字超详解指针

【附下载】密码获取工具LaZagne安装及使用

Symlink(): solution to protocol error in PHP artisan storage:link on win10

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

Qt+vtk+occt reading iges/step model
随机推荐
Vulnhub's cereal
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
vulnhub之Ripper
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
Php Export word method (One MHT)
vulnhub之pyexp
ES6 standard
[combinatorics] permutation and combination (example of permutation and combination)
Vulnhub's Tomato (tomato)
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
Shutter widget: centerslice attribute
Integer string int mutual conversion
Shell: basic learning
Develop plug-ins for idea
PHP导出word方法(一mht)
New features of ES6
Vulnhub pyexp
vulnhub之momentum
Interview experience in summer camp of Central South University in 2022
STL tutorial 8-map