使用TreadPool时的ThreadLocal示例
生活随笔
收集整理的這篇文章主要介紹了
使用TreadPool时的ThreadLocal示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 }
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
總結
以上是生活随笔為你收集整理的使用TreadPool时的ThreadLocal示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: w3wp trace文件Debug
- 下一篇: [转] GIS算法源码集合