在多線程訪問的時候,同一時刻只能有一個線程能夠用 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