mytask.java

来自「java 完全探索的随书源码」· Java 代码 · 共 42 行

JAVA
42
字号
import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class MyTask extends TimerTask{  int maxNumberOfTimesToRun = 0;  // A reference to a date object  Date currentDateTime = null;  static int counter = 1;  Timer myTimer = null;  // Default Constructor  public MyTask( int maxCounter, Timer aTimer )  {    super();    maxNumberOfTimesToRun = maxCounter;    myTimer = aTimer;  }  // Override the abstract method method  public void run()  {    if ( MyTask.counter <= maxNumberOfTimesToRun )    {      System.out.println( MyTask.counter );      // Create a current date instance      currentDateTime = new Date( System.currentTimeMillis() );      // Display the current date object as a string      System.out.println( "MyTask: " + currentDateTime.toString() );      MyTask.counter++;    }    else    {      // Since we reached the counter max, cancel the task      cancel();      // Also cancel the timer since I'm the only one using it.      // This might not be true in all cases      myTimer.cancel();    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?