ThreadLocal 就是将某个变量隔离起来,只让一个线程使用。
示例:代码来源
package yjmyzz.test;
public class ThreadLocalTest1 {
public static class MyRunnable implements Runnable {
private ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
@Override
public void run() {
threadLocal.set((int) (Math.random() * 100D));
System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable(), "A");
Thread t2 = new Thread(new MyRunnable(), "B");
t1.start();
t2.start();
}
}
运行结果:
B:48
A:32