日期:2014-05-20 浏览次数:20759 次
import java.util.Timer; import java.util.TimerTask; public class MachineTimerTask extends Thread { private int a; private static int count; public void start(){ super.start(); Timer timer=new Timer(true); TimerTask task=new TimerTask(){ public void run() { reset(); try { sleep(1000); }catch(InterruptedException e){ } } }; timer.schedule(task,10,500); //setting timer task } 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 { MachineTimerTask machine = new MachineTimerTask(); machine.start(); } }
timer.schedule(task,10,500);
timer.schedule(task,10);
timer.schedule(task1,0,1000); timer.schedule(task2,0,500);