package sy;
import java.util.Random;

public class random
{

public static void main(String[] args)
{
// TODO Auto-generated method stub
ROOM room = new ROOM();
room.student1.start();
room.student2.start();
room.teacher.start();
}

}

class ROOM implements Runnable
{
Thread teacher, student1, student2;

ROOM()
{
teacher = new Thread(this);
student1 = new Thread(this);
student2 = new Thread(this);
teacher.setName("王校长");
student1.setName("Theshy");
student2.setName("Rookie");
}
@Override
public void run() {
// TODO Auto-generated method stub
if (Thread.currentThread() == student1)
{
try
{
System.out.println(student1.getName() + "想睡20分钟");
Thread.sleep(1000 * 20 * 60);
}catch (InterruptedException e)
{
System.out.println(student1.getName() + "被老师叫醒了4次.");
}
System.out.println(student1.getName() + "开始唤醒Rookie");
System.out.println(student1.getName() + "兄弟你醒一醒!!!");
student2.interrupt();//吵醒2
//System.out.println(student2.getName() + "被Theshy叫醒...");
}
else if (Thread.currentThread() == teacher)
{
for (int i = 1; i <= 4; i++)
{
System.out.println("上课!");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
student1.interrupt(); // 吵醒student
}
else if (Thread.currentThread() == student2)
{
try
{
System.out.println(student2.getName() + "想睡1小时");
Thread.sleep(1000 * 60 * 60);
}catch (InterruptedException e)
{
System.out.println(student2.getName() + "被Theshy叫醒了");
}
//System.out.println(student2.getName() + "被Theshy叫醒...");
}
}

}

package sy;

public class buytickets
{ public static void main(String[] args)
{
// TODO Auto-generated method stub
ROOM room = new ROOM();
room.p1.start();
room.p2.start();
room.p3.start();
room.shoper.start();
} }
class ROOM implements Runnable
{
Thread p1, p2, p3, shoper; ROOM()
{
p1 = new Thread(this);
p2 = new Thread(this);
p3 = new Thread(this);
shoper = new Thread(this);
shoper.setName("售票员");
p1.setName("张三");
p2.setName("李四");
p3.setName("王五");
} @Override
public void run()
{
// TODO Auto-generated method stub
if (Thread.currentThread() == shoper)
{
try
{
System.out.println("售货员营业");
Thread.sleep(1000 * 60 * 60);
}
catch (InterruptedException e)
{
System.out.println(shoper.getName() + "结束");
}
}
else if(Thread.currentThread() == p1)
{
try
{
System.out.println("张三需要等待");
Thread.sleep(1000 * 60 * 60);
}catch (InterruptedException e)
{
System.out.println(p1.getName() + "拿到票和40元找零");
}
p3.interrupt();
}
else if(Thread.currentThread() == p2)
{
System.out.println(p2.getName()+"拿到票和得到找零10");
p1.interrupt();
}
else if(Thread.currentThread() == p3)
{
try{
System.out.println("王五拿到票");
Thread.sleep(1000 * 60 * 60);
}
catch (InterruptedException e)
{
System.out.println("售货员无票,手中有50+10元");
}
shoper.interrupt();
}
} private void notify(Thread p12) {
// TODO Auto-generated method stub }
}
05-11 11:23