?? machine.java
字號:
package usetimer;
import java.util.Timer;
import java.util.TimerTask;
public class Machine extends Thread{
private int a;
private static int count;
public void start(){
super.start();
Timer timer=new Timer(true); //把與Timer關(guān)聯(lián)的線程設(shè)為后臺線程
TimerTask task=new TimerTask(){ //匿名類
public void run(){
while(true){ //無限循環(huán)
reset();
try{
sleep(1000);
}catch(InterruptedException e){/*throw new RuntimeException(e);*/}
}
}
};
timer.schedule(task,10,500); //設(shè)置定時任務(wù)
}
public void reset(){a=0;}
public void run(){
while(true){
System.out.println(getName()+":"+a++);
if(count++==100)break;
yield();
}
}
public static void main(String args[])throws Exception{
Machine machine=new Machine();
machine.start();
}
}
/****************************************************
* 作者:孫衛(wèi)琴 *
* 來源:<<Java面向?qū)ο缶幊?gt;> *
* 技術(shù)支持網(wǎng)址:www.javathinker.org *
***************************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -