package concurrent.threadlocal;
/**
* ThreadLocal测试
*
* @author cakin
*/
public class ThreadLocalSimpleTest {
private static ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> "Alex");
/**
* JVM start main thread
*
* @param args 启动参数
*/
public static void main(String[] args) throws InterruptedException {
threadLocal.set("Alex2");
Thread.sleep(1000);
String value = threadLocal.get();
System.out.println(value);
}
}
Alex2
package concurrent.threadlocal;
import java.util.Random;
/**
* ThreadLocal测试
*
* @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());
}
}
Thread-T2:Thread-T2
Thread-T1:Thread-T1
main:null
package concurrent.threadlocal;
import java.util.Random;
/**
* 模拟 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());
}
}
package concurrent.threadlocal;
import java.util.Random;
/**
* 模拟 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());
}
}
Thread-T2:Thread-T2
Thread-T1:Thread-T1
main:No Value
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/chengqiuming/article/details/124331537
内容来源于网络,如有侵权,请联系作者删除!