이클립스
원문:
 
Timer timer = new Timer(); // 타이머 생성
timer.scheduleAtFixedRate(timerTask, new Date(), 10*1000 );  // 현재시간부터 10초마다 timerTask 실행
timer.cancel();  //타이머 중지

이런 식으로 할 수 있다.
위에서 timerTask 는 java.util.TimerTask 라는 추상 객체를 상속받아 만들 수 있다.

public class MyTimerTask extends TimerTask {
    public void run() {
        System.out.println("Hello!");
    }
}

위는 아주 간단한 사용법이고...
내가 응용해서 사용하는 방법을 하나 소개한다.
내가 하고자 하는 것은 일정 시간마다 무엇인가를 돌려야 하는데...
그 무엇인가는 동적으로 추가 또는 제거가 될 수 있다.
Timer 는 무한 쓰레드이기 때문에 시작되었으면 누군가 반드시 cancel() 메쏘드로 종료해야 한다.
즉, 누군가가 타이머의 시작과 종료를 관리를 해주어야 하는데...
나는 그 무엇인가가 들어오면 타이머가 시작되고, 무엇인가가 아무것도 없으면 타이머가 중지되도록 하고 싶다.

그 이슈로 해서 만든 소스는 다음과 같다...

public class MyTimer extends TimerTask  {
    private static MyTimer SINGLETON;
    public synchronized static MyTimer getInstance() {
        if ( SINGLETON == null ) SINGLETON = new MyTimer();
        return SINGLETON;
    }

    private Timer timer;
    private Vector taskList;
    private long delay;

    private MyTimer() {
        this.taskList = new Vector();
        this.timer = null;
        this.delay = 10*1000; // 10초
    }

    public void insertTask(Runnable task,int idx) {
        this.taskList.insertElementAt(task,idx);
        if ( this.timer == null ) startTimer();
    }
    public void appendTask(Runnable task) {
        this.taskList.insertElementAt(task,idx);
        if ( this.timer == null ) startTimer();
    }
    public void removeTask(Runnable task) {
        taskList.removeelement(task);
        if ( taskList.size() == 0 ) stopTimer();
    }
    public boolean containsTask(Runnable task) {
        return taskList.contains(task);
    }

    public void run() {
        if ( taskList.size()>0 ) {
            for(int i=0; i<taskList.size(); i++) {
                Runnable task = (Runnable) taskList.elementAt(i);
                task.run(); 
                // 앞의 Task가 를 종료되고, 다음 Task를 실행하는 방식이라 new Thread(task).start() 하지 않았다.
                // 용도에 맞추어 입맛대로...
            }
        } else {
             stopTimer();
        }
    }

    private void startTimer() {
        if ( this.timer != null ) stopTimer();
        this.timer = new Timer();
        this.timer.scheduleAtFixedRate(this,new Date(),delay);
    }
    private void stopTimer() {
        if ( this.timer != null ) {
            this.timer.cancel();
            this.timer = null;
        }
    }

    // 테스트 해볼까..?
    public static void main(String[] args) {
        try {
             MyTimer timer = MyTimer.getInstance();
             Runnable task = new Runnable() {
                 public void run() {
                     System.out.println("Hello!");
                 }
             }
             timer.appendTask(task);
             Thread.currentThread().sleep(60 * 1000);  // 1분 동안 timer 가 도는 걸 확인하고...
             timer.removeTask(task);
        } catch(Throwable t) {
             t.printStackTrace();
        }
    }
}