java多线程------实现Runnable接口创建多进程,实现资源共享

//实现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+"张票");
			}
		}
	}
}

你可能感兴趣的:(java)