浅谈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