如果不用runnable的方式,用Thread方式。volatile怎么用?
package com.wxl.thread;
public class KeyPersonThread extends Thread {
volatile boolean keepRunning = true;
public void run() {
System.out.println(getName()+"开始战斗!");
while(keepRunning) {
System.out.println(getName()+"左突右杀,攻击隋军...");
}
System.out.println(getName()+"结束战斗!");
}
}
比如我上面keyPerson继承Thread,使用volatile boolean keepRunning = true; 在主线程中,mrCheng.keepRunning=false;会编译不通过!!!