public class TestTeacher {
public static void main(String[] args){
ClassRoom room = new ClassRoom();
Thread teacher = new Thread(room);
Thread zhangxiao = new Thread(room);
Thread zhengming = new Thread(room);
teacher.setName("LiuLaoShi");
zhangxiao.setName("ZhaonXiao");
zhengming.setName("ZhengMing");
zhangxiao.start();
zhengming.start();
teacher.start();
}
}
class ClassRoom implements Runnable{
Thread zhangxiao,zhengming,teacher;
public void run(){
Thread thread = Thread.currentThread();
if(thread == zhangxiao || thread == zhengming){
try{
System.out.println(thread.getName() + " rest 10 minues ");
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println(thread.getName() + "cao");
}
System.out.println(thread.getName() + "Good morning");
}
else if(thread == teacher){
for(int i = 0;i < 3;i++){
System.out.println(thread.getName() + "say : go to class" );
try{
Thread.sleep(500);
}catch(InterruptedException e){}
}
zhangxiao.interrupt();
zhengming.interrupt();
}
}
}
------解决思路----------------------
楼主将代码改成这样就知道了
Thread thread = Thread.currentThread();
System.out.println("zhangxiao=" + zhangxiao);
System.out.println("zhengming=" + zhengming);
if (thread == zhangxiao
------解决思路----------------------
thread == zhengming)
楼主上面定义的是 Thread zhangxiao,zhengming,teacher;
定义了三个线程变量,但是并没有初始化
也就是 定义了三个为null 的线程,下面自然是不可能相等的了