其实要实现楼主的想法很简单,就差一个覆盖Thread的一个构造函数。
class Ticket implements Runnable
{
private int ticket=100;
public void run()
{
while(true)
{
if(ticket>0)
{
System.out.println(Thread.currentThread().getName()+"----sale--"+ ticket--);//这里我想显示我定义的线程名称的信息,该怎么修改
}
}
}
}
class SubThread extends Thread
{
SubThread(Runnable target,String name)//这个构造函数可查API,一般传一个Runnable子类对象的时候比较多,其实还可以在这里定义线程的名称
{
super(target,name);
}
}class TicketDemo
{
public static void main(String[] args)
{
Ticket t=new Ticket();//创建多个线程怎么给线程分别弄名字
Thread f=new SubThread(t,"线程f");
Thread f1=new SubThread(t,"线程f1");
Thread f2=new SubThread(t,"线程f2");
Thread f3=new SubThread(t,"线程f3");
f.start();
f1.start();
f2.start();
f3.start();
}
} |