Java並發編程--線程局部變數使用

來源:互聯網
上載者:User

標籤:

       共用資料是並發程式最核心的問題之一。下面我們看一個Deom感受一下多線程對屬性的影響。

代碼:

UnsafeTask.java

package com.tgb.klx.thread;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.TimeUnit;public class UnsafeTask implements Runnable {private String startDate;@Overridepublic void run() {SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");startDate=sdf.format( new  Date());System.out.println("開始線程:"+Thread.currentThread().getId()+",開始的時間:"+startDate);try {TimeUnit.SECONDS.sleep((int)Math.rint(Math.random()*10));} catch (InterruptedException e) {e.printStackTrace();}System.out.println("結束線程:"+Thread.currentThread().getId()+",開始的時間:"+startDate);}}

Core.java

package com.tgb.klx.thread;import java.util.concurrent.TimeUnit;public class Core {public static void main(String[] args) {UnsafeTask task=new UnsafeTask();for(int i=0;i<10;i++){Thread thread=new Thread(task);thread.start();try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}}}}


    運行結果:


    可以看到,最後一個線程開始的時間,是剩餘線程結束的時間,明顯的一個線程啟動的時間被其他線程修改了。

 



    那麼,我們通過什麼方式能保證一個線程的局部變數不會被其他線程修改呢?下面我們使用ThreadLocal,來保護相線程的屬性不被其他線程修改:

代碼:

 SafeTask.java

package com.tgb.klx.thread;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.TimeUnit;public class SafeTask implements Runnable {private static ThreadLocal<String> startDate=new ThreadLocal<String>(){protected String initialValue(){SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return sdf.format( new  Date());}};@Overridepublic void run() {System.out.println("開始線程:"+Thread.currentThread().getId()+",開始的時間:"+startDate.get());try {TimeUnit.SECONDS.sleep((int)Math.rint(Math.random()*10));} catch (InterruptedException e) {e.printStackTrace();}System.out.println("結束線程:"+Thread.currentThread().getId()+",開始的時間:"+startDate.get());}}

運行結果:

 

     我們保證了線程內的局部變數的安全性。ThreadLocal是JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal為解決多線程程式的並發問題提供了一種新的思路。使用這個工具類可以很簡潔地編寫出優美的多線程程式,ThreadLocal並不是一個Thread,而是Thread的局部變數。


Java並發編程--線程局部變數使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.