class SubRunnable implements Runnable
{
Thread stu1,stu2,stu3;
public SubRunnable()
{
stu1=new Thread(this);
stu2=new Thread(this);
stu3=new Thread(this);
}
public void run()
{
if(Thread.currentThread()==stu1)
{
try
{
System.out.println("第一个学生睡觉了");
stu1.sleep(60000); //第一个了睡10分钟
}
catch(InterruptedException e)
{
System.out.println("第一个学生醒了并叫醒第三个同学");
stu3.interrupt(); //中断线程stu3
}
}
else if(Thread.currentThread()!=stu1)
{
try
{
System.out.println("第二、三个学生睡觉了");
stu2.sleep(120000); //第二个睡了20分钟
stu3.sleep(180000); //第三个睡了30分钟
}
catch(InterruptedException e)
{
System.out.println("第二个学生醒了");
}
}
}
}
public class Ex
{
public static void main(String[] args)
{
SubRunnable s=new SubRunnable();
s.stu1.start();
s.stu2.start();
SubRunnable tech=new SubRunnable();
//tech.stu1.interrupt();
//tech.stu2.interrupt();
s.stu1.interrupt();
s.stu2.interrupt();
}
}
{
Thread stu1,stu2,stu3;
public SubRunnable()
{
stu1=new Thread(this);
stu2=new Thread(this);
stu3=new Thread(this);
}
public void run()
{
if(Thread.currentThread()==stu1)
{
try
{
System.out.println("第一个学生睡觉了");
stu1.sleep(60000); //第一个了睡10分钟
}
catch(InterruptedException e)
{
System.out.println("第一个学生醒了并叫醒第三个同学");
stu3.interrupt(); //中断线程stu3
}
}
else if(Thread.currentThread()!=stu1)
{
try
{
System.out.println("第二、三个学生睡觉了");
stu2.sleep(120000); //第二个睡了20分钟
stu3.sleep(180000); //第三个睡了30分钟
}
catch(InterruptedException e)
{
System.out.println("第二个学生醒了");
}
}
}
}
public class Ex
{
public static void main(String[] args)
{
SubRunnable s=new SubRunnable();
s.stu1.start();
s.stu2.start();
SubRunnable tech=new SubRunnable();
//tech.stu1.interrupt();
//tech.stu2.interrupt();
s.stu1.interrupt();
s.stu2.interrupt();
}
}