当前位置:网站首页>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();
}
}
边栏推荐
- PHP export word method (one MHT)
- 4000字超详解指针
- 网络通讯之Socket-Tcp(一)
- [combinatorics] permutation and combination (example of permutation and combination)
- DNS multi-point deployment IP anycast+bgp actual combat analysis
- Flutter Widget : Flow
- 242. Effective letter heteronyms
- PHP export word method (phpword)
- Dart: view the dill compiled code file
- MySQL searches and sorts out common methods according to time
猜你喜欢
随机推荐
How to convert a numeric string to an integer
Laravel time zone timezone
"Jianzhi offer 04" two-dimensional array search
STL Tutorial 9 deep copy and shallow copy of container elements
OpenGL 索引缓存对象EBO和线宽模式
ES6新特性
vulnhub之pyexp
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.".
Qt OpenGL 纹理贴图
OpenGL index cache object EBO and lineweight mode
Ripper of vulnhub
Shutter: overview of shutter architecture (excerpt)
Dart: about grpc (I)
vulnhub之tomato(西红柿)
Differences between MySQL Union and union all
Wrong arrangement (lottery, email)
How to deploy web pages to Alibaba cloud
laravel 时区问题timezone
SLF4J 日志门面
Vulnhub's Nagini