您的位置:首页 > 其它

多线程(一)——通过实现Runnable接口创建线程

2013-06-18 06:50 573 查看
/*
	The second way of creating a thread.
	Implement Runnable interface,processes as following:
	1.Define a class and implement Runnable interface;
	2.Rewrite the run method in Runnable interface;
	3.Create a object through Thread class;
	4.Throw the son-class object of Runnable to constructor
	  of Thread class as real argument;	
	5.Call the start method of Thread class so the Thread 
	  will start and call Runnable interface.
*/

class Ticket implements Runnable
{
	private int tick = 100;
	public void run()
	{
		while(true)
		{
			if(tick>0)
			{
				System.out.println(Thread.currentThread().getName()+"------sale:"+tick--);
			}
		}
	}
}

class TicketDemo2
{
	public static void main(String[] args)
	{
		Ticket t = new Ticket();
		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		Thread t3 = new Thread(t);
		Thread t4 = new Thread(t);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐