import java.util.Random;
public class ThreadLocalTest {
static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 123);
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
Random random = new Random();
while (true) {
int nextInt = random.nextInt();
threadLocal.set(nextInt);
System.out.println("nextInt:" + nextInt + "=" + threadLocal.get());
try {
Thread.currentThread().sleep(200);
}
ThreadLocal把变量绑定到线程
最新推荐文章于 2024-03-05 08:38:40 发布