淺談Java多線程同步機制之同步塊(辦法)——synchronized。本站提示廣大學習愛好者:(淺談Java多線程同步機制之同步塊(辦法)——synchronized)文章只能為提供參考,不一定能成為您想要的結果。以下是淺談Java多線程同步機制之同步塊(辦法)——synchronized正文
在多線程訪問的時分,同一時辰只能有一個線程可以用 synchronized 修飾的辦法或許代碼塊,處理了資源共享。上面代碼表示三個窗口購5張火車票:
1 package com.jikexueyuan.thread;
2 /*
3 * 未運用synchronized,存在並發
4 */
5 class RunnableDemo implements Runnable{
6 private int tickets = 5;
7 @Override
8 public void run() {
9 for (int i = 0; i < 10; i++) {
10 try {
11 Thread.sleep(500);
12 } catch (InterruptedException e) {
13 e.printStackTrace();
14 }
15 if (tickets>0) {
16 System.out.println("車票: "+tickets--);
17 }
18 }
19
20 }
21 }
22
23 public class ThreadTest {
24
25 public static void main(String[] args) {
26 RunnableDemo r = new RunnableDemo();
27 Thread t1 = new Thread(r);
28 Thread t2 = new Thread(r);
29 Thread t3 = new Thread(r);
30 t1.start();
31 t2.start();
32 t3.start();
33 }
34
35 }
其中一次的運轉後果:
車票: 5 車票: 4 車票: 3 車票: 2 車票: 1 車票: 2
運用synchronized同步塊後:
1 package com.jikexueyuan.thread;
2 /*
3 * 運用synchronized塊
4 */
5 class RunnableDemo implements Runnable{
6 private int tickets = 5;
7 @Override
8 public void run() {
9 for (int i = 0; i < 10; i++) {
10 try {
11 Thread.sleep(500);
12 } catch (InterruptedException e) {
13 e.printStackTrace();
14 }
15 synchronized (this) {
16 if (tickets>0) {
17 System.out.println("車票: "+tickets--);
18 }
19 }
20 }
21 }
22 }
23
24 public class ThreadTest {
25
26 public static void main(String[] args) {
27 RunnableDemo r = new RunnableDemo();
28 Thread t1 = new Thread(r);
29 Thread t2 = new Thread(r);
30 Thread t3 = new Thread(r);
31 t1.start();
32 t2.start();
33 t3.start();
34 }
35
36 }
運用synchronized同步辦法:
1 package com.jikexueyuan.thread;
2 /*
3 * 運用synchronized同步辦法
4 */
5 class RunnableDemo implements Runnable{
6 private int tickets = 5;
7 @Override
8 public void run() {
9 for (int i = 0; i < 10; i++) {
10 show();
11 }
12 }
13 public synchronized void show() {
14 if (tickets>0) {
15 System.out.println("車票: "+tickets--);
16 }
17 }
18 }
19
20 public class ThreadTest {
21
22 public static void main(String[] args) {
23 RunnableDemo r = new RunnableDemo();
24 Thread t1 = new Thread(r);
25 Thread t2 = new Thread(r);
26 Thread t3 = new Thread(r);
27 t1.start();
28 t2.start();
29 t3.start();
30 }
31
32 }
無論運用synchronized同步塊還是同步辦法,運轉後果均為合理後果:
車票: 5 車票: 4 車票: 3 車票: 2 車票: 1
考慮:volatile是另一種同步機制,能否可以呢?參考我的鏈接文章:Java實際與理論:正確運用Volatile變量 http://www.ibm.com/developerworks/cn/java/j-jtp06197.html