测试
package com;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import test.B;
public class Main implements Runnable {
public static B b = new B();
public static int i =0;
public void run() {
b.setB((i++)+"");
value.set(b);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(value.get());
}
public static ThreadLocal<B> value = new ThreadLocal<B>() {};
public static void main(String[] args) {
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
for(int i=0;i<5;i++){
newCachedThreadPool.execute(new Main());
}
other.get();
// Main main1 = new Main();
// main1.run();
// Main main2 = new Main();
// main2.run();
}
}
浙公网安备 33010602011771号