JAvaマルチスレッド------Runnableインタフェースを実現マルチプロセスを作成し、資源共有を実現

711 ワード

//  Runnable       ,      
package xian_cheng;

public class Example05 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TickWindow tw=new TickWindow();//     tw
		new Thread(tw,"   1").start();//           ,    
		new Thread(tw,"   2").start();
		//new Thread(tw,"   3").start();
		//new Thread(tw,"   4").start();
	}

}
class TickWindow implements Runnable{
	private int tickets=100;
	public void run(){
		while (true) {
			if (tickets>0) {
				Thread th=Thread.currentThread();//      
				String th_name=th.getName();//        
				System.out.println(th_name+"     "+--tickets+"  ");
			}
		}
	}
}