当前位置:网站首页>ThreadLocal actual combat
ThreadLocal actual combat
2022-04-22 08:53:00 【chengqiuming】
One A simple example
1 Code
package concurrent.threadlocal;
/**
* ThreadLocal test
*
* @author cakin
*/
public class ThreadLocalSimpleTest {
private static ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> "Alex");
/**
* JVM start main thread
*
* @param args Launch parameters
*/
public static void main(String[] args) throws InterruptedException {
threadLocal.set("Alex2");
Thread.sleep(1000);
String value = threadLocal.get();
System.out.println(value);
}
}
2 test
Alex2
Two Complex examples
1 Code
package concurrent.threadlocal;
import java.util.Random;
/**
* ThreadLocal test
*
* @author cakin
*/
public class ThreadLocalComplexTest {
private static final ThreadLocal<String> threadLocal = new ThreadLocal<>();
private static final Random RANDOM = new Random(System.currentTimeMillis());
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
threadLocal.set("Thread-T1");
try {
Thread.sleep(RANDOM.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}, "Thread-T1");
Thread t2 = new Thread(() -> {
threadLocal.set("Thread-T2");
try {
Thread.sleep(RANDOM.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}, "Thread-T2");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}
}
2 test
Thread-T2:Thread-T2
Thread-T1:Thread-T1
main:null
3、 ... and simulation ThreadLocal
1 simulation ThreadLocal
package concurrent.threadlocal;
import java.util.Random;
/**
* simulation ThreadLocal
*
* @author cakin
*/
public class ThreadLocalSimulatorTest {
private static final ThreadLocalSimulator<String> threadLocal = new ThreadLocalSimulator<String>() {
@Override
public String initialValue() {
return "No Value";
}
};
private static final Random RANDOM = new Random(System.currentTimeMillis());
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
threadLocal.set("Thread-T1");
try {
Thread.sleep(RANDOM.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}, "Thread-T1");
Thread t2 = new Thread(() -> {
threadLocal.set("Thread-T2");
try {
Thread.sleep(RANDOM.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}, "Thread-T2");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}
}
2 Test code
package concurrent.threadlocal;
import java.util.Random;
/**
* simulation ThreadLocal
*
* @author cakin
*/
public class ThreadLocalSimulatorTest {
private static final ThreadLocalSimulator<String> threadLocal = new ThreadLocalSimulator<String>() {
@Override
public String initialValue() {
return "No Value";
}
};
private static final Random RANDOM = new Random(System.currentTimeMillis());
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
threadLocal.set("Thread-T1");
try {
Thread.sleep(RANDOM.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}, "Thread-T1");
Thread t2 = new Thread(() -> {
threadLocal.set("Thread-T2");
try {
Thread.sleep(RANDOM.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}, "Thread-T2");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}
}
3 test
Thread-T2:Thread-T2
Thread-T1:Thread-T1
main:No Value
版权声明
本文为[chengqiuming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220802006934.html
边栏推荐
- ZAB协议
- 工业缺陷检测项目实战(三)——基于FPN_Tensorflow的PCB缺陷检测
- py技能树
- 机器学习之逻辑回归的步骤原理
- Solve the problem that the disk has space but cannot create files --- repair the server file system
- Flume 组成,Put 事务,Take 事务
- Restore MySQL service after computer reset
- Elastic job installation deployment access
- CPU memory access space
- 解决磁盘有空间但创建不了文件---修复服务器文件系统
猜你喜欢
随机推荐
Scope and lifecycle of variables
RHEL user and group management - Notes
悬空else问题
机器学习之逻辑回归的步骤原理
Vs compiler annotation style
varchar与char有什么区别?
Differences between ROM, ram, SRAM, DRAM, flash and SDRAM
SQL window function
ROM, Ram, SRAM, DRAM, flash, SDRAM Difference
Operation steps for mysqlbin log playback
详解转义字符
==And equals
布尔类型【bool】
变量的作用域和生命周期
Quick sequencing and optimization
The pit encountered in the compilation and installation of pinpoint of win system will be shared with you
Nessus漏洞扫描简介
Solution access denied for user 'root' @ 'localhost' (using password: yes)
CSDN(Markdown模式)如何实现:页内跳转
Tencent video automatic check-in detailed version (V value obtained by multiple methods)




![Usage of static [detailed explanation]](/img/97/7c4030d7729f00eaf5eda9ffd5a4f1.png)




