SOAR 发表于 2013-5-26 18:47
public class DormitoryHead extends Thread{
private String dorName;//宿舍名
public DormitoryHead ...
你这个写的感觉有问题啊!和题目的意思不符合,下面是我刚刚写的,不知道对不对!不过还是要谢谢你。public class Demo
{
public static void main(String args[])
{
Person p1 = new Person();
Thread t1 = new Thread(p1,"a寝室");
Thread t2 = new Thread(p1,"b寝室");
Thread t3 = new Thread(p1,"c寝室");
t1.start();
t2.start();
t3.start();
}
}
class Person implements Runnable
{
public void run()
{
for(int x=1;x<5;x++)
{
synchronized(this){
try
{
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + "的" + x + "正在登记");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
|