Android多線程研究(6)——多線程之間資料隔離

來源:互聯網
上載者:User

標籤:thread   java   threadlocal   

在上一篇《Android多線程研究(5)——線程之間共用資料》中對線程之間的資料共用進行了學習和研究,這一篇我們來看看如何解決多個線程之間的資料隔離問題,什麼是資料隔離呢?比如說我們現在開啟了兩個線程,這兩個線程都要同時給同一個全域變數data賦值,各個線程操作它賦值後的變數資料,這裡就需要用到隔離。先看一段代碼:

import java.util.Random;public class ThreadLocalTest {private static int data = 0;public static void main(String[] args) {for(int i=0; i<2; i++){new Thread(new Runnable() {@Overridepublic void run() {data = new Random().nextInt();System.out.println(Thread.currentThread().getName() +" has put data: " + data);new A().get();new B().get();}}).start();}}static class A{public int get(){System.out.println("A from " + Thread.currentThread().getName() +" has get data: " + data);return data;}}static class B{public int get(){System.out.println("B from " + Thread.currentThread().getName() +" has get data: " + data);return data;}}}
運行結果:

從上面我們可以看到Thread-0和Thread-1都在操作變數data,但是兩個線程之間沒有做到對資料操作的隔離,所以輸出結果中兩個線程共用了一個data變數。

我們將上面代碼修改如下:

import java.util.HashMap;import java.util.Map;import java.util.Random;public class ThreadLocalTest {//private static int data = 0;private static Map<Thread, Integer> map = new HashMap<Thread, Integer>();public static void main(String[] args) {for(int i=0; i<2; i++){new Thread(new Runnable() {@Overridepublic void run() {//data = new Random().nextInt();int data = new Random().nextInt();map.put(Thread.currentThread(), data);System.out.println(Thread.currentThread().getName() +" has put data: " + data);new A().get();new B().get();}}).start();}}static class A{public int get(){System.out.println("A from " + Thread.currentThread().getName() +" has get data: " + map.get(Thread.currentThread()));return map.get(Thread.currentThread());}}static class B{public int get(){System.out.println("B from " + Thread.currentThread().getName() +" has get data: " + map.get(Thread.currentThread()));return  map.get(Thread.currentThread());}}}
輸出結果:

上面代碼中我們用一個Map集合隔離了線程對data資料的操作,其實相當於建立了一個data資料的備份(雙份的data)實現了線程之間資料的隔離,其實早在Java 1.2就引入了一個用來支援線程資料隔離的類(java.lang.ThreadLocal),下面我們來看看如何使用ThreadLocal實現線程之間的資料隔離。

ThreadLocal中的三個方法:‘

get() :返回當前線程的線程局部變數副本

protected initialValue() :返回該線程局部變數的當前線程的初始值

void set(Object value) :設定當前線程的線程局部變數副本的值

其中initialValue方法是為子類寫的方法,在一個線程第一次調用get()或者set()方法時執行,並且僅執行一次。

import java.util.Random;public class ThreadLocalTest {//private static int data = 0;//private static Map<Thread, Integer> map = new HashMap<Thread, Integer>();private static ThreadLocal<Integer> tl = new ThreadLocal<Integer>();public static void main(String[] args) {for(int i=0; i<2; i++){new Thread(new Runnable() {@Overridepublic void run() {//data = new Random().nextInt();int data = new Random().nextInt();//map.put(Thread.currentThread(), data);tl.set(data);System.out.println(Thread.currentThread().getName() +" has put data: " + data);new A().get();new B().get();}}).start();}}static class A{public int get(){System.out.println("A from " + Thread.currentThread().getName() +" has get data: " + tl.get());return tl.get();}}static class B{public int get(){System.out.println("B from " + Thread.currentThread().getName() +" has get data: " + tl.get());return  tl.get();}}}
運行結果:

上面代碼明顯少了很多,其實ThreadLocal中底層也是用Map來儲存變數副本實現的。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.