张晓华的博客

        2005年,最后一场雪,还是下了,虽然凄凉了一些!
posts - 172, comments - 92, trackbacks - 5, articles - 1

用java.util.Timer类定时运行程序

Posted on Wednesday, February 16, 2005 7:40 PM
用java.util.Timer类定时运行程序

package test;

import java.util.Timer;
import java.util.TimerTask;

public class EggTimer {
    private final Timer timer = new Timer();
    private final int minutes;

    public EggTimer(int minutes) {
        this.minutes = minutes;
    }

    public void start() {
        timer.schedule(new TimerTask() {
            public void run() {
                playSound();
                timer.cancel();
            }
            private void playSound() {
                System.out.println("Your egg is ready!");
                // Start a new thread to play a sound...
            }
        }, minutes * 60 * 1000);
    }

    public static void main(String[] args) {
        EggTimer eggTimer = new EggTimer(2);
        eggTimer.start();
    }

}

Feedback

# re: 用java.util.Timer类定时运行程序

6/7/2005 3:25 AM by vivi
I call back it

# re: 用java.util.Timer类定时运行程序

9/14/2007 4:14 AM by xelloss
please insert marginalia with english or chinese, i can not understand it!

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: