原文地址:http://blog.csdn.net/hejingyuan6/article/details/47053409#
多線程共享數據的方式:
1,如果每個線程執行的代碼相同,可以使用同一個Runnable對象,這個Runnable對象中有那個共享數據,例如,賣票系統就可以這么做。
2,如果每個線程執行的代碼不同,這時候需要用不同的Runnable對象,例如,設計4個線程。其中兩個線程每次對j增加1,另外兩個線程對j每次減1,銀行存取款
有兩種方法來解決此類問題:
將共享數據封裝成另外一個對象,然后將這個對象逐一傳遞給各個Runnable對象,每個線程對共享數據的操作方法也分配到那個對象身上完成,這樣容易實現針對數據進行各個操作的互斥和通信
將Runnable對象作為一個類的內部類,共享數據作為這個類的成員變量,每個線程對共享數據的操作方法也封裝在外部類,以便實現對數據的各個操作的同步和互斥,作為內部類的各個Runnable對象調用外部類的這些方法。
每個線程執行的代碼相同,可以使用同一個Runnable對象
賣票系統demo:
package com.xujishou; public class SellTicket { /** * @param args */ public static void main(String[] args) { Ticket t = new Ticket(); new Thread(t).start(); new Thread(t).start(); } } class Ticket implements Runnable { private int ticket = 10; public void run() { while (ticket > 0) { ticket--; System.out.println("當前票數為:" + ticket); } } }
執行
簡單的多線程間數據共享,每個線程執行的代碼不同,用不同的Runnable對象
設計4個線程。
其中兩個線程每次對j增加1,另外兩個線程對j每次減1
package com.xujishou; public class TestThread { /** * @param args */ public static void main(String[] args) { final MyData data = new MyData(); for (int i = 0; i < 10; i++) { new Thread(new Runnable() { public void run() { data.add(); } }).start(); new Thread(new Runnable() { public void run() { data.dec(); } }).start(); } } } class MyData { private int j = 0; public synchronized void add() { j++; System.out.println("線程" + Thread.currentThread().getName() + "j為:" + j); } public synchronized void dec() { j--; System.out.println("線程" + Thread.currentThread().getName() + "j為:" + j); } }
銀行存取款實例:
package com.xujishou; public class Acount { private int money; public Acount(int money) { this.money = money; } public synchronized void getMoney(int money) { // 注意這個地方必須用while循環,因為即便再存入錢也有可能比取的要少 while (this.money < money) { System.out.println("取款:" + money + " 余額:" + this.money + " 余額不足,正在等待存款......"); try { wait(); } catch (Exception e) { } } this.money = this.money - money; System.out.println("取出:" + money + " 還剩余:" + this.money); } public synchronized void setMoney(int money) { try { Thread.sleep(10); } catch (Exception e) { } this.money = this.money + money; System.out.println("新存入:" + money + " 共計:" + this.money); notify(); } public static void main(String args[]) { Acount Acount = new Acount(0); Bank b = new Bank(Acount); Consumer c = new Consumer(Acount); new Thread(b).start(); new Thread(c).start(); } } // 存款類 class Bank implements Runnable { Acount Acount; public Bank(Acount Acount) { this.Acount = Acount; } public void run() { while (true) { int temp = (int) (Math.random() * 1000); Acount.setMoney(temp); } } } // 取款類 class Consumer implements Runnable { Acount Acount; public Consumer(Acount Acount) { this.Acount = Acount; } public void run() { while (true) { int temp = (int) (Math.random() * 1000); Acount.getMoney(temp); } } }
總結:
其實多線程間的共享數據最主要的還是互斥,多個線程共享一個變量,針對變量的操作實現原子性即可。
原文地址:http://blog.csdn.net/hejingyuan6/article/details/47053409#
多線程共享數據的方式:
1,如果每個線程執行的代碼相同,可以使用同一個Runnable對象,這個Runnable對象中有那個共享數據,例如,賣票系統就可以這么做。
2,如果每個線程執行的代碼不同,這時候需要用不同的Runnable對象,例如,設計4個線程。其中兩個線程每次對j增加1,另外兩個線程對j每次減1,銀行存取款
有兩種方法來解決此類問題:
將共享數據封裝成另外一個對象,然后將這個對象逐一傳遞給各個Runnable對象,每個線程對共享數據的操作方法也分配到那個對象身上完成,這樣容易實現針對數據進行各個操作的互斥和通信
將Runnable對象作為一個類的內部類,共享數據作為這個類的成員變量,每個線程對共享數據的操作方法也封裝在外部類,以便實現對數據的各個操作的同步和互斥,作為內部類的各個Runnable對象調用外部類的這些方法。
下面逐一介紹
每個線程執行的代碼相同,可以使用同一個Runnable對象
賣票系統demo:
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
|
package
com.xujishou;
public
class
SellTicket {
/**
* @param args
*/
public
static
void main(String[] args) {
Ticket t =
new
Ticket();
new
Thread(t).start();
new
Thread(t).start();
}
}
class
Ticket
implements
Runnable {
private
int ticket = 10;
public void run() {
while
(ticket > 0) {
ticket--;
System.out.println(
"當前票數為:"
+ ticket);
}
}
}
|
執行
簡單的多線程間數據共享,每個線程執行的代碼不同,用不同的Runnable對象
設計4個線程。
其中兩個線程每次對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
36
37
38
39
40
41
42
43
44
|
package
com.xujishou;
public
class
TestThread {
/**
* @param args
*/
public
static
void main(String[] args) {
final MyData data =
new
MyData();
for
(int i = 0; i < 10; i++) {
new
Thread(
new
Runnable() {
public void run() {
data.add();
}
}).start();
new
Thread(
new
Runnable() {
public void run() {
data.dec();
}
}).start();
}
}
}
class
MyData {
private
int j = 0;
public synchronized void add() {
j++;
System.out.println(
"線程"
+ Thread.currentThread().getName() +
"j為:"
+ j);
}
public synchronized void dec() {
j--;
System.out.println(
"線程"
+ Thread.currentThread().getName() +
"j為:"
+ j);
}
}
|
銀行存取款實例:
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
|
package
com.xujishou;
public
class
Acount {
private
int money;
public Acount(int money) {
this
.money = money;
}
public synchronized void getMoney(int money) {
// 注意這個地方必須用while循環,因為即便再存入錢也有可能比取的要少
while
(
this
.money < money) {
System.out.println(
"取款:"
+ money +
" 余額:"
+
this
.money
+
" 余額不足,正在等待存款......"
);
try
{
wait();
}
catch
(Exception e) {
}
}
this
.money =
this
.money - money;
System.out.println(
"取出:"
+ money +
" 還剩余:"
+
this
.money);
}
public synchronized void setMoney(int money) {
try
{
Thread.sleep(10);
}
catch
(Exception e) {
}
this
.money =
this
.money + money;
System.out.println(
"新存入:"
+ money +
" 共計:"
+
this
.money);
notify();
}
public
static
void main(String args[]) {
Acount Acount =
new
Acount(0);
Bank b =
new
Bank(Acount);
Consumer c =
new
Consumer(Acount);
new
Thread(b).start();
new
Thread(c).start();
}
}
// 存款類
class
Bank
implements
Runnable {
Acount Acount;
public Bank(Acount Acount) {
this
.Acount = Acount;
}
public void run() {
while
(
true
) {
int temp = (int) (Math.random() * 1000);
Acount.setMoney(temp);
}
}
}
// 取款類
class
Consumer
implements
Runnable {
Acount Acount;
public Consumer(Acount Acount) {
this
.Acount = Acount;
}
public void run() {
while
(
true
) {
int temp = (int) (Math.random() * 1000);
Acount.getMoney(temp);
}
}
}
|
總結:
其實多線程間的共享數據最主要的還是互斥,多個線程共享一個變量,針對變量的操作實現原子性即可。