在多线程访问的时候,同一时刻只能有一个线程能够用 synchronized 修饰的方法或者代码块,解决了资源共享。下面代码示意三个窗口购5张火车票:
package com.jikexueyuan.thread;
/*
* 未使用synchronized,存在并发
*/
class RunnableDemo implements Runnable{
private int tickets = 5;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (tickets>0) {
System.out.println("车票: "+tickets--);
}
} }
} public class ThreadTest { public static void main(String[] args) {
RunnableDemo r = new RunnableDemo();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
} }
其中一次的运行结果:
车票: 5
车票: 4
车票: 3
车票: 2
车票: 1
车票: 2
使用synchronized同步块后:
package com.jikexueyuan.thread;
/*
* 使用synchronized块
*/
class RunnableDemo implements Runnable{
private int tickets = 5;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this) {
if (tickets>0) {
System.out.println("车票: "+tickets--);
}
}
}
}
} public class ThreadTest { public static void main(String[] args) {
RunnableDemo r = new RunnableDemo();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
} }
使用synchronized同步方法:
package com.jikexueyuan.thread;
/*
* 使用synchronized同步方法
*/
class RunnableDemo implements Runnable{
private int tickets = 5;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
show();
}
}
public synchronized void show() {
if (tickets>0) {
System.out.println("车票: "+tickets--);
}
}
} public class ThreadTest { public static void main(String[] args) {
RunnableDemo r = new RunnableDemo();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
} }
无论使用synchronized同步块还是同步方法,运行结果均为合理结果:
车票: 5
车票: 4
车票: 3
车票: 2
车票: 1
思考:volatile是另一种同步机制,是否可以呢?参考我的链接文章:Java理论与实践:正确使用Volatile变量 http://www.ibm.com/developerworks/cn/java/j-jtp06197.html