/*
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();
}
}
多线程(一)——通过实现Runnable接口创建线程
最新推荐文章于 2024-04-06 23:53:23 发布