当前位置:网站首页>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();
}
}
边栏推荐
- [combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
- vulnhub之cereal
- 网络通讯之Socket-Tcp(一)
- 在网上炒股开户可以吗?资金安全吗?
- vulnhub之tomato(西红柿)
- 023(【模板】最小生成树)(最小生成树)
- Wrong arrangement (lottery, email)
- OpenGL shader use
- Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
- DNS multi-point deployment IP anycast+bgp actual combat analysis
猜你喜欢
vulnhub之momentum
OpenGL index cache object EBO and lineweight mode
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Qt OpenGL 旋转、平移、缩放
vulnhub之GeminiInc v2
Shutter widget: centerslice attribute
Momentum of vulnhub
[learning notes] DP status and transfer
CGroup introduction
Wechat applet - basic content
随机推荐
C language improvement article (wchar_t) character type
Socket TCP for network communication (I)
Solution à la défaillance de l'installation d'Electron
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
ES6新特性
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
Qt OpenGL 纹理贴图
Differences between MySQL Union and union all
Niuniu's team competition
Systemverilog-- OOP--对象的拷贝
Itext7 uses iexternalsignature container for signature and signature verification
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
During FTP login, the error "530 login incorrect.login failed" is reported
Qt OpenGL相机的使用
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
OpenGL index cache object EBO and lineweight mode
XML (DTD, XML parsing, XML modeling)
Wrong arrangement (lottery, email)
Pki/ca and digital certificate
ArcGIS application (XXI) ArcMap method of deleting layer specified features