class Ticket implements Runnable//实现Runnable的接口
{
private int tick = 100;//定义票数为100张
public void run()//覆盖接口中的run方法
{
for(int x=1;x<=100;x++)
{ if (tick >0)
{
System.out.println(Thread.currentThread().getName()+"..nima..");
tick--;
x++;
}
if(x==100)
break;
}
}
}
class ThreadTest02
{
public static void main(String[] args)
{
Ticket t = new Ticket();//创建Ticket类中的对象
Thread t1 = new Thread(t);//创建线程,并将Ticket类中的对象引用到线程中
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();//启动线程
t2.start();
t3.start();
t4.start();
}
}
|
-
2.jpg
(54.24 KB, 下载次数: 45)
|