阅读背景:

Java多线程--同步与死锁:synchronized;等待与唤醒:wait、notify、notifyAll;生命周期

来源:互联网 

1、问题的引出


class MyThread implements Runnable{
	private int ticket = 5 ;	// 假设一共有5张票
	public void run(){
		for(int i=0;i<100;i++){
			if(ticket>0){	// 还有票
				try{
					Thread.sleep(300) ;	// 加入延迟
				}catch(InterruptedException e){
					e.printStackTrace() ;
				}
				System.out.println("卖票:ticket = " + ticket-- );
			}
		}
	}
};
public class SyncDemo01{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 定义线程对象
		Thread t1 = new Thread(mt) ;	// 定义Thread对象
		Thread t2 = new Thread(mt) ;	// 定义Thread对象
		Thread t3 = new Thread(mt) ;	// 定义Thread对象
		t1.start() ;
		t2.start() ;
		t3.start() ;
	}
};class MyThread impl



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: