线程Thread

版权声明:JAVA https://blog.csdn.net/weixin_43190126/article/details/84961422
package com.qyl.maipiao.one;

import java.util.concurrent.locks.ReentrantLock;
public class elderly extends Thread {

	static int count = 50;
	 ReentrantLock lock = new ReentrantLock();
	static Object obj = new Object();
	@Override
	public void run() {
		while (count>0) {			
			lock.lock();
					if(count>0){
				try {	
					System.out.println(getName()+"售出第"+count);
					count--;
				} finally {
					lock.unlock();
				}
		  }				
		}
	}
}
package com.qyl.maipiao.one;

public class count extends Exception {
	
	public static void main(String[] args) {
		elderly el = new elderly();
		elderly e2 = new elderly();
		elderly e3 = new elderly();
		
		el.setName("老人");
		e2.setName("小孩");
		e3.setName("成人");
		
		el.start();
		e2.start();
		e3.start();
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43190126/article/details/84961422