Java多线程同步机制之同步块(方法)——synchronized
2017-01-11 16:31
411 查看
在多线程访问的时候,同一时刻只能有一个线程能够用 synchronized 修饰的方法或者代码块,解决了资源共享。下面代码示意三个窗口购5张火车票:
其中一次的运行结果:
使用synchronized同步块后:
使用synchronized同步方法:
无论使用synchronized同步块还是同步方法,运行结果均为合理结果:
思考:volatile是另一种同步机制,是否可以呢?参考链接文章:Java理论与实践:正确使用Volatile变量 http://www.ibm.com/developerworks/cn/java/j-jtp06197.html
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
相关文章推荐
- 浅谈Java多线程同步机制之同步块(方法)——synchronized
- java synchronized同步方法调用另一个同步方法,锁机制问题
- java 多线程10:synchronized锁机制 之 锁定类静态方法 和锁定类.Class 和 数据String的常量池特性 同步静态方法
- 关于Java同步机制中synchronized同步方法
- Java同步机制浅谈――synchronized对代码作何影响?
- Java同步机制浅谈――synchronized对代码作何影响?
- java---synchronized同步机制
- Java同步机制收集总结--synchronized
- Java同步机制浅谈――synchronized对代码作何影响?
- 浅谈Java同步机制synchronized对代码作何影响?
- Java同步机制浅谈――synchronized对代码作何影响?(转)
- Java 多线程同步 锁机制与synchronized
- Java同步机制浅谈 synchronized
- Java同步机制浅谈――synchronized对代码作何影响?
- Java线程同步机制synchronized关键字的理解
- 转载:Java同步机制浅谈――synchronized对代码作何影响?
- Java同步机制浅谈――synchronized对代码作何影响?
- java同步机制——synchronized
- java 同步机制浅谈-----synchronized对代码作何影响?
- Java同步机制浅谈―synchronized