为什么setIsrunning(false)方法的调用者是“主”线程

时间:2016-03-07 02:43:50

标签: java multithreading

public class Run {
public static void main(String[] args) {
    try{
        RunThreas runThreas=new RunThreas();
        runThreas.start();
        //Thread.sleep(2000);
        runThreas.setIsrunning(false);
        System.out.println("已经赋值为false");
    }catch (Exception e){
        e.printStackTrace();
    }
}
}


class RunThreas extends Thread{

private boolean isrunning=true;

public boolean isRunning(){
    return isrunning;
}

public void setIsrunning(boolean isrunning){
    System.out.println(Thread.currentThread().getName()+"set");
    this.isrunning=isrunning;
}

@Override
public void run() {
    super.run();
    System.out.println(Thread.currentThread().getName()+"run");
    System.out.println("进入run");
    while(isrunning){
        System.out.println("i am running");
    }

    System.out.println("线程被停止了");
}
}

setIsrunnning()方法属于Runthread,所以我认为它应该由Runthread的instanse调用,但实际上它是由Thread“main”调用的。我想知道这是为什么?

0 个答案:

没有答案