1 package tk.mysweetie.java.test;
2
3 import java.util.concurrent.ArrayBlockingQueue;
4 import java.util.concurrent.ThreadPoolExecutor;
5 import java.util.concurrent.TimeUnit;
6
7 /**
8 * @author wangzhengyang.pt
9 * 使用TreadPool时的ThreadLocal示例
10 */
11 public class TestThreadPool {
12
13 private static ThreadLocal<Boolean> bol = new ThreadLocal<Boolean>();
14
15 /**
16 * 第二个提交的Runnable没有对TreadLocal进行set,但是已经被set过了
17 */
18 public static void main(String[] args) {
19
20 ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 100, TimeUnit. SECONDS ,
21 new ArrayBlockingQueue<Runnable>(1), new ThreadPoolExecutor.AbortPolicy() );
22
23 //进行set的Runnable
24 pool.execute(new Runnable() {
25 @Override
26 public void run() {
27 /***set为true***/
28 bol.set(true);
29 System.out.println("" + Thread.currentThread() + "seted bol:" + bol.get());
30 }
31 });
32
33 //没有set的Runnable
34 pool.execute(new Runnable() {
35 @Override
36 public void run() {
37 System.out.println("" + Thread.currentThread() + "not seted bol:" + bol.get());
38 }
39 });
40 }
41 }
转载于:https://www.cnblogs.com/goofy/archive/2011/11/11/2245907.html