package cn.sasa.demo1;
import java.util.concurrent.ExecutionException;
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException, ExecutionException {
//创建Runnable接口实现类对象
Ticket t = new Ticket();
//创建3个Thread类对象,传递Runnable接口实现类
Thread t0 = new Thread(t);
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
t0.start();
t1.start();
t2.start();
t3.start();
/**
* 线程安全问题一般发生在:
* 多线程并发访问同一个数据资源
*
* 没有加同步,通过线程休眠,出现了安全问题
*
* Thread-0 出售第 99
* Thread-1 出售第 98
* Thread-2 出售第 100
* Thread-3 出售第 97
* Thread-1 出售第 96
* Thread-0 出售第 96
* Thread-3 出售第 95
* Thread-2 出售第 94
* Thread-0 出售第 93
* Thread-1 出售第 92
* Thread-3 出售第 91
* Thread-2 出售第 91
* Thread-0 出售第 90
* Thread-1 出售第 90
* Thread-2 出售第 88
* Thread-3 出售第 89
* Thread-0 出售第 87
* Thread-1 出售第 87 ......
*
* 解决安全问题: 线程同步
* synchronized(任意对象){
* 线程要操作的共享数据
* }
*
*/
}
}
package cn.sasa.demo1;
public class Ticket implements Runnable {
//定义出售的票源
private int ticket = 100;
//同步锁,对象监视器
//没有锁的线程不能执行,只能等
//
private Object obj = new Object();
public void run(){
int flag = 100;
while(flag > 0){
//加入同步代码块,保证线程安全
synchronized(obj) {
if( ticket > 0){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" 出售第 "+ticket--);
}
flag --;
}
}
}
}