package duoxiancheng;
class Ticket implements Runnable//实现接口Runnable方式创建多线程
{
int ticket=100;
public void run()//覆盖run方法
{
while(true)
{
if(ticket>0)
System.out.println(Thread.currentThread().getName()+"..."+ticket--);//用Thread类中的currentThread
} //方法返回当前执行线程对象的引用
} //getName()获取当前线程的名称
}
public class ThreadTicketDemo {
public static void main(String [] args)
{
Ticket t=new Ticket();
Thread d1=new Thread(t);
Thread d2=new Thread(t);
Thread d3=new Thread(t);
d1.start();
d2.start();
d3.start();
}
}
(多线程)ThreadTicketDemo
最新推荐文章于 2022-12-24 21:37:24 发布