1、線程范圍的共享變量
多個業務模塊針對同一個static
變量的操作 要保證在不同線程中 各模塊操作的是自身對應的變量對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public class ThreadScopeSharaData { private static int data = 0 ; public static void main(String[] args) { for ( int i = 0 ;i< 2 ;i++){ new Thread( new Runnable(){ @Override public void run() { data = new Random().nextInt(); System.out.println(Thread.currentThread().getName()+ " put random data:" +data); new A().get() ; new B().get() ; } }).start() ; } } static class A { public int get(){ System.out.println( "A from " + Thread.currentThread().getName() + " get data :" + data); return data ; } } static class B{ public int get(){ System.out.println( "B from " + Thread.currentThread().getName() + " get data :" + data); return data ; } } } |
模塊A ,B都需要訪問static
的變量data 在線程0中會隨機生成一個data值 假設為10 那么此時模塊A和模塊B在線程0中得到的data
的值為10 ;在線程1中 假設會為data賦值為20 那么在當前線程下
模塊A和模塊B得到data的值應該為20
看程序執行的結果:
Thread-0 put random data:-2009009251
Thread-1 put random data:-2009009251
A from Thread-0 get data :-2009009251
A from Thread-1 get data :-2009009251
B from Thread-0 get data :-2009009251
B from Thread-1 get data :-2009009251
Thread-0 put random data:-2045829602
Thread-1 put random data:-1842611697
A from Thread-0 get data :-1842611697
A from Thread-1 get data :-1842611697
B from Thread-0 get data :-1842611697
B from Thread-1 get data :-1842611697
會出現兩種情況:
- 1.由于線程執行速度,新的隨機值將就的隨機值覆蓋 data 值一樣
- 2.data 值不一樣,但 A、B線程都
2、使用Map實現線程范圍內數據的共享
可是將data數據和當前允許的線程綁定在一塊,在模塊A和模塊B去獲取數據data的時候 是通過當前所屬的線程去取得data的結果就行了。
聲明一個Map集合 集合的Key為Thread 存儲當前所屬線程 Value 保存data的值,
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class ThreadScopeSharaData { private static Map<Thread, Integer> threadData = new HashMap<>(); public static void main(String[] args) { for ( int i = 0 ; i < 2 ; i++) { new Thread( new Runnable() { @Override public void run() { int data = new Random().nextInt(); System.out.println(Thread.currentThread().getName() + " put random data:" + data); threadData.put(Thread.currentThread(), data); new A().get(); new B().get(); } }).start(); } } static class A { public void get() { int data = threadData.get(Thread.currentThread()); System.out.println( "A from " + Thread.currentThread().getName() + " get data:" + data); } } static class B { public void get() { int data = threadData.get(Thread.currentThread()); System.out.println( "B from " + Thread.currentThread().getName() + " get data:" + data); } } } |
Thread-0 put random data:-123490895
Thread-1 put random data:-1060992440
A from Thread-0 get data:-123490895
A from Thread-1 get data:-1060992440
B from Thread-0 get data:-123490895
B from Thread-1 get data:-1060992440
3、ThreadLocal實現線程范圍內數據的共享
(1)訂單處理包含一系列操作:減少庫存量、增加一條流水臺賬、修改總賬,這幾個操作要在同一個事務中完成,通常也即同一個線程中進行處理,如果累加公司應收款的操作失敗了,則應該把前面的操作回滾,否則,提交所有操作,這要求這些操作使用相同的數據庫連接對象,而這些操作的代碼分別位于不同的模塊類中。
(2)銀行轉賬包含一系列操作: 把轉出帳戶的余額減少,把轉入帳戶的余額增加,這兩個操作要在同一個事務中完成,它們必須使用相同的數據庫連接對象,轉入和轉出操作的代碼分別是兩個不同的帳戶對象的方法。
(3)例如Strut2
的ActionContext
,同一段代碼被不同的線程調用運行時,該代碼操作的數據是每個線程各自的狀態和數據,對于不同的線程來說,getContext
方法拿到的對象都不相同,對同一個線程來說,不管調用getContext
方法多少次和在哪個模塊中getContext
方法,拿到的都是同一個。
(4)實驗案例:定義一個全局共享的ThreadLocal
變量,然后啟動多個線程向該ThreadLocal變量中存儲一個隨機值,接著各個線程調用另外其他多個類的方法,這多個類的方法中讀取這個ThreadLocal
變量的值,就可以看到多個類在同一個線程中共享同一份數據。
(5)實現對ThreadLocal
變量的封裝,讓外界不要直接操作ThreadLocal
變量。
- 對基本類型的數據的封裝,這種應用相對很少見。
- 對對象類型的數據的封裝,比較常見,即讓某個類針對不同線程分別創建一個獨立的實例對象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class ThreadLocalTest { private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>(); public static void main(String[] args) { for ( int i = 0 ; i < 2 ; i++) { new Thread( new Runnable() { @Override public void run() { int data = new Random().nextInt(); System.out.println(Thread.currentThread().getName() + " put random data:" + data); threadLocal.set(data); new A().get(); new B().get(); } }).start(); } } static class A { public void get() { int data = threadLocal.get(); System.out.println( "A from " + Thread.currentThread().getName() + " get data:" + data); } } static class B { public void get() { int data = threadLocal.get(); System.out.println( "B from " + Thread.currentThread().getName() + " get data:" + data); } } } |
Thread-0 put random data:-2015900409
Thread-1 put random data:-645411160
A from Thread-0 get data:-2015900409
A from Thread-1 get data:-645411160
B from Thread-0 get data:-2015900409
B from Thread-1 get data:-645411160
4、優化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
public class ThreadLocalTest { private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>(); //private static ThreadLocal<MyThreadScopeData> myThreadScopeDataThreadLocal = new ThreadLocal<>(); public static void main(String[] args) { for ( int i = 0 ; i < 2 ; i++) { new Thread( new Runnable() { @Override public void run() { int data = new Random().nextInt(); System.out.println(Thread.currentThread().getName() + " put random data:" + data); threadLocal.set(data); // MyThreadScopeData myThreadScopeData = new MyThreadScopeData(); // myThreadScopeData.setName("name" + data); // myThreadScopeData.setAge(data); // myThreadScopeDataThreadLocal.set(myThreadScopeData); //獲取與當前線程綁定的實例并設置值 MyThreadScopeData.getThreadInstance().setName( "name" + data); MyThreadScopeData.getThreadInstance().setAge(data); new A().get(); new B().get(); } }).start(); } } static class A { public void get() { int data = threadLocal.get(); // MyThreadScopeData myData = myThreadScopeDataThreadLocal.get(); // // // System.out.println("A from " + Thread.currentThread().getName() // + " getMyData: " + myData.getName() + "," + myData.getAge()); MyThreadScopeData myData = MyThreadScopeData.getThreadInstance(); System.out.println( "A from " + Thread.currentThread().getName() + " getMyData: " + myData.getName() + "," + myData.getAge()); } } static class B { public void get() { int data = threadLocal.get(); //System.out.println("B from " + Thread.currentThread().getName() + " get data:" + data); MyThreadScopeData myData = MyThreadScopeData.getThreadInstance(); System.out.println( "B from " + Thread.currentThread().getName() + " getMyData: " + myData.getName() + "," + myData.getAge()); } } } //一個綁定當前線程的類 class MyThreadScopeData { private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<>(); private String name; private int age; private MyThreadScopeData() { } //定義一個靜態方法,返回各線程自己的實例 //這里不必用同步,因為每個線程都要創建自己的實例,所以沒有線程安全問題。 public static MyThreadScopeData getThreadInstance() { //獲取當前線程綁定的實例 MyThreadScopeData instance = map.get(); if (instance == null ) { instance = new MyThreadScopeData(); map.set(instance); } return instance; } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
Thread-1 put random data:-1041517189
Thread-0 put random data:-98835751
A from Thread-1 getMyData: name-1041517189,-1041517189
A from Thread-0 getMyData: name-98835751,-98835751
B from Thread-1 getMyData: name-1041517189,-1041517189
B from Thread-0 getMyData: name-98835751,-98835751
5、實例
設計4個線程,其中兩個線程每次對j增加1,另外兩個線程對j每次減少1,寫出程序。
(1)如果每個線程執行的代碼相同,可以使用同一個Runnable
對象,這個Runnable對象中有那個共享數據,例如,賣票系統就可以這么做。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class SellTicket { //賣票系統,多個窗口的處理邏輯是相同的 public static void main(String[] args) { Ticket t = new Ticket(); new Thread(t).start(); new Thread(t).start(); } } /** * 將屬性和處理邏輯,封裝在一個類中 * * @author yang */ class Ticket implements Runnable { private int ticket = 10 ; public synchronized void run() { while (ticket > 0 ) { ticket--; System.out.println( "當前票數為:" + ticket); } } } |
(2)如果每個線程執行的代碼不同,這時候需要用不同的Runnable
對象,例如,設計2個線程。一個線程對j增加1,另外一個線程對j減1,銀行存取款系統。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class MultiThreadShareData { private int j; public static void main(String[] args) { MultiThreadShareData multiThreadShareData = new MultiThreadShareData(); for ( int i= 0 ;i< 2 ;i++){ new Thread(multiThreadShareData. new ShareData1()).start(); //增加 new Thread(multiThreadShareData. new ShareData2()).start(); //減少 } } //自增 private synchronized void Inc(){ j++; System.out.println(Thread.currentThread().getName()+ " inc " +j); } //自減 private synchronized void Dec(){ j--; System.out.println(Thread.currentThread().getName()+ " dec " +j); } class ShareData1 implements Runnable { public void run() { for ( int i= 0 ;i< 5 ;i++){ Inc(); } } } class ShareData2 implements Runnable { public void run() { for ( int i= 0 ;i< 5 ;i++){ Dec(); } } } } |
Thread-0 inc 1
Thread-0 inc 2
Thread-0 inc 3
Thread-0 inc 4
Thread-0 inc 5
Thread-1 dec 4
Thread-1 dec 3
Thread-2 inc 4
Thread-2 inc 5
Thread-2 inc 6
Thread-2 inc 7
Thread-2 inc 8
Thread-1 dec 7
Thread-1 dec 6
Thread-1 dec 5
Thread-3 dec 4
Thread-3 dec 3
Thread-3 dec 2
Thread-3 dec 1
Thread-3 dec 0
到此這篇關于Java 多線程之間共享數據的文章就介紹到這了,更多相關Java 多線程共享數據內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/7016934881129136159