生活随笔
收集整理的這篇文章主要介紹了
Java并发编程—ThreadLocal用法详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、用法
ThreadLocal用于保存某個線程共享變量:對于同一個static ThreadLocal,不同線程只能從中g(shù)et,set,remove自己的變量,而不會影響其他線程的變量。
ThreadLocal.get: 獲取ThreadLocal中當(dāng)前線程共享變量的值。ThreadLocal.set: 設(shè)置ThreadLocal中當(dāng)前線程共享變量的值。ThreadLocal.remove: 移除ThreadLocal中當(dāng)前線程共享變量的值。ThreadLocal.initialValue: ThreadLocal沒有被當(dāng)前線程賦值時或當(dāng)前線程剛調(diào)用remove方法后調(diào)用get方法,返回此方法值。
package?com.coshaho.reflect;public?class?MyThreadLocal{private?static?final?ThreadLocal<Object> threadLocal =?new?ThreadLocal<Object>(){//ThreadLocal沒有被當(dāng)前線程賦值時或當(dāng)前線程剛調(diào)用remove方法后調(diào)用get方法,返回此方法值@Overrideprotected?Object initialValue(){System.out.println("調(diào)用get方法時,當(dāng)前線程共享變量沒有設(shè)置,調(diào)用initialValue獲取默認(rèn)值!");return?null;}};public?static?void?main(String[] args){new?Thread(new?MyIntegerTask("IntegerTask1")).start();new?Thread(new?MyStringTask("StringTask1")).start();new?Thread(new?MyIntegerTask("IntegerTask2")).start();new?Thread(new?MyStringTask("StringTask2")).start();}public?static?class?MyIntegerTask?implements?Runnable{private?String name;MyIntegerTask(String name){this.name = name;}@Overridepublic?void?run(){for(int?i =?0; i <?5; i++){// ThreadLocal.get方法獲取線程變量if(null?== MyThreadLocal.threadLocal.get()){// ThreadLocal.et方法設(shè)置線程變量MyThreadLocal.threadLocal.set(0);System.out.println("線程"?+ name +?": 0");}else{int?num = (Integer)MyThreadLocal.threadLocal.get();MyThreadLocal.threadLocal.set(num +?1);System.out.println("線程"?+ name +?": "?+ MyThreadLocal.threadLocal.get());if(i ==?3){MyThreadLocal.threadLocal.remove();}}try{Thread.sleep(1000);}catch?(InterruptedException e){e.printStackTrace();}}??}?}public?static?class?MyStringTask?implements?Runnable{private?String name;MyStringTask(String name){this.name = name;}@Overridepublic?void?run(){for(int?i =?0; i <?5; i++){if(null?== MyThreadLocal.threadLocal.get()){MyThreadLocal.threadLocal.set("a");System.out.println("線程"?+ name +?": a");}else{String str = (String)MyThreadLocal.threadLocal.get();MyThreadLocal.threadLocal.set(str +?"a");System.out.println("線程"?+ name +?": "?+ MyThreadLocal.threadLocal.get());}try{Thread.sleep(800);}catch?(InterruptedException e){e.printStackTrace();}}}}
}
運(yùn)行結(jié)果如下:
| 調(diào)用get方法時,當(dāng)前線程共享變量沒有設(shè)置,調(diào)用initialValue獲取默認(rèn)值! 線程IntegerTask1: 0 調(diào)用get方法時,當(dāng)前線程共享變量沒有設(shè)置,調(diào)用initialValue獲取默認(rèn)值! 線程IntegerTask2: 0 調(diào)用get方法時,當(dāng)前線程共享變量沒有設(shè)置,調(diào)用initialValue獲取默認(rèn)值! 調(diào)用get方法時,當(dāng)前線程共享變量沒有設(shè)置,調(diào)用initialValue獲取默認(rèn)值! 線程StringTask1: a 線程StringTask2: a 線程StringTask1: aa 線程StringTask2: aa 線程IntegerTask1: 1 線程IntegerTask2: 1 線程StringTask1: aaa 線程StringTask2: aaa 線程IntegerTask2: 2 線程IntegerTask1: 2 線程StringTask2: aaaa 線程StringTask1: aaaa 線程IntegerTask2: 3 線程IntegerTask1: 3 線程StringTask1: aaaaa 線程StringTask2: aaaaa 調(diào)用get方法時,當(dāng)前線程共享變量沒有設(shè)置,調(diào)用initialValue獲取默認(rèn)值! 線程IntegerTask2: 0 調(diào)用get方法時,當(dāng)前線程共享變量沒有設(shè)置,調(diào)用initialValue獲取默認(rèn)值! 線程IntegerTask1: 0 |
總結(jié)
以上是生活随笔為你收集整理的Java并发编程—ThreadLocal用法详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。