java实现多线程(车站卖票)

import java.util.ArrayList;
import java.util.List;

public class 多线程 {
//	public static int j=0;
	public static long a=0;
	public static void main(String[] args) throws InterruptedException {
		List<Thread> list = new ArrayList<Thread>();
		System.out.println(System.currentTimeMillis());
		 a=System.currentTimeMillis();//开始运行计时器
		 
		aa a1 = new aa();
		Thread aa1= new Thread(a1,"1号");;//都共用一个,分成不同的线程
		Thread aa2=	new Thread(a1,"2号");;
		Thread aa3=	new Thread(a1,"3号");;
		Thread aa4=	new Thread(a1,"4号");;
		Thread aa5=	new Thread(a1,"5号");;
		list.add(aa1);//把每一个线程都放入里面
		list.add(aa2);
		list.add(aa3);
		list.add(aa4);
		list.add(aa5);
		aa1.setPriority(10);//设置线程的优先级
		aa2.setPriority(10);
		aa3.setPriority(10);
		aa4.setPriority(10);
		aa5.setPriority(10);
		aa1.start();//开始每一个线程
		aa2.start();
		aa3.start();
		aa4.start();
		aa5.start();
		
//		for (int i = 0; i < 4; i++) {
//			list.get(i).join();
//		}
//	System.out.println(System.currentTimeMillis());
		Thread currtime = new Thread(new cc());
		currtime.setPriority(1);
		currtime.start();//结束运行计时器
		System.out.println("546545645645646546468468461654654654654");
//	System.out.println(j);
	}

}
class aa implements Runnable{
//这是车票,调用的时候分为不同的车站口出票
//	多线程 a = new 多线程();
	private int b =1000;
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(b>0) {
			Thread th = Thread.currentThread();
			String s = th.getName();
			System.out.println(s+"正在出售第"+b--+"张票");
		}
	}
	
}

class cc implements Runnable{
//计时
	@Override
	public void run() {
		多线程 a = new 多线程();
		// TODO Auto-generated method stub
		System.out.println(System.currentTimeMillis()-a.a);
	}
	
}
class bb implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i < 1000000; i++) {
			int j=1+1+1+1;
		}
	}
	
}

原文地址:https://www.cnblogs.com/a1439775520/p/13076300.html