中国开发网: 论坛: 程序员情感CBD: 贴子 325311
nostalgic
which one is better?
我现在要每隔一段时间读取表里的数据,用哪种方式要好些?

import java.util.*;



public class TimePrinter implements Runnable
{

int pauseTime;

String name;

public TimePrinter(int x, String n)
{

pauseTime = x;

name = n;

}



public void run()
{

while(true)
{

try
{

System.out.println(name + ":" + new

Date(System.currentTimeMillis()));

Thread.sleep(pauseTime);

}
catch(Exception e)
{

System.out.println(e);

}

}

}



public static void main(String args[])
{

Thread t1 =
new Thread(new TimePrinter
(1000, "Fast Guy"));

t1.start();

Thread t2 =
new Thread(new TimePrinter
(3000, "Slow Guy"));

t2.start();



}

}
***************************************************************

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(1);
eggTimer.start();
}

}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录