Main.java
public class Main {
public static void main(String[] args) {
Service ser=new Service();
ThreadA a=new ThreadA(ser);
a.start();
ser.stopMethod();
}
}
ThreadA.java
public class ThreadA extends Thread {
private Service service;
public ThreadA(Service service)
{
this.service=service;
}
@Override
public void run() {
service.runMethod();
}
}
Service.java
public class Service {
private boolean isContinueRun=true;
public void runMethod()
{
while(isContinueRun)
{
}
System.out.println("停下来了");
}
public void stopMethod()
{
isContinueRun=false;
}
}
按照书上说的 不是共享变量在多线程中是单独的副本吗 ser已经传入到ThreadA中 我在主线程中改变了状态值会影响到线程中的副本吗? 这段代码执行后 确实把循环停住了
求大神们帮忙看看