日期:2014-05-20 浏览次数:20743 次
public class MonitoringTimer{
public static boolean flag=false;
public boolean getFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void run(){
this.setFlag(true);
while(flag){
System.out.println("a");
try{
Thread.sleep(3000);
}catch(Exception e){
e.printStackTrace();
}
}
}
public void stop(){
this.setFlag(false);
}
public static void main(String[] args) {
MonitoringTimer m=new MonitoringTimer();
m.run();
}
}
public class MonitoringTimer extends Thread {
public MonitoringTimer() {
this.start();
}
public volatile boolean flag = false;
public boolean getFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void run() {
this.setFlag(true);
while (flag) {
System.out.println("a");
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void stopMonitor() {
this.setFlag(false);
}
public static void main(String[] args) throws InterruptedException {
MonitoringTimer m = new MonitoringTimer();
Thread.sleep(10000);
// 主线程调用关闭方法
m.stopMonitor();
}
}