最近我一直在使用大号循环来打印Hello World
:
int counter = 0;
while(true) {
//loop for ~5 seconds
for(int i = 0; i < 2147483647 ; i++) {
//another loop because it's 2012 and PCs have gotten considerably faster :)
for(int j = 0; j < 2147483647 ; j++){ ... }
}
System.out.println(counter + ". Hello World!");
counter++;
}
我知道这是一种非常愚蠢的方法,但我还没有在Java中使用过任何计时器库。如何修改上述内容,每隔3秒打印一次?
答案 0 :(得分:179)
如果要执行定期任务,请使用ScheduledExecutorService
。具体来说是ScheduledExecutorService.scheduleAtFixedRate
代码:
Runnable helloRunnable = new Runnable() {
public void run() {
System.out.println("Hello world");
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 3, TimeUnit.SECONDS);
答案 1 :(得分:161)
您还可以查看Timer
和TimerTask
类,您可以使用它们安排任务每n
秒运行一次。
您需要一个扩展TimerTask
的类并覆盖public void run()
方法,每次将该类的实例传递给timer.schedule()
方法时都会执行该方法。
这是一个例子,每5秒打印一次Hello World
: -
class SayHello extends TimerTask {
public void run() {
System.out.println("Hello World!");
}
}
// And From your main() method or any other method
Timer timer = new Timer();
timer.schedule(new SayHello(), 0, 5000);
答案 2 :(得分:31)
尝试这样做:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Hello World");
}
}, 0, 5000);
此代码将每 5000 毫秒( 5 秒)运行打印到控制台 Hello World 。 有关详细信息,请阅读https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html
答案 3 :(得分:12)
我用计时器搞清楚,希望它有所帮助。我使用了来自同一个软件包的java.util.Timer
和TimerTask
的计时器。见下文:
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("Hello World");
}
};
Timer timer = new Timer();
timer.schedule(task, new Date(), 3000);
答案 4 :(得分:9)
在内部使用Thread.sleep(3000)进行循环
答案 5 :(得分:4)
public class HelloWorld extends TimerTask{
public void run() {
System.out.println("Hello World");
}
}
public class PrintHelloWorld {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new HelloWorld(), 0, 5000);
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("InterruptedException Exception" + e.getMessage());
}
}
}
}
创建无限循环广告调度程序任务已配置。
答案 6 :(得分:3)
最简单的方法是将主线程设置为休眠3000毫秒(3秒):
for(int i = 0; i< 10; i++) {
try {
//sending the actual Thread of execution to sleep X milliseconds
Thread.sleep(3000);
} catch(InterruptedException ie) {}
System.out.println("Hello world!"):
}
这将使线程停止至少X毫秒。该线程可能正在休眠更多时间,但这取决于JVM。唯一保证的是线程将至少休眠那几毫秒。看一下Thread#sleep
doc:
使当前正在执行的线程休眠(暂时停止执行)指定的毫秒数,受制于系统计时器和调度程序的精度和准确性。
答案 7 :(得分:3)
使用java.util.Timer
和Timer#schedule(TimerTask,delay,period)
方法可以为您提供帮助。
public class RemindTask extends TimerTask {
public void run() {
System.out.println(" Hello World!");
}
public static void main(String[] args){
Timer timer = new Timer();
timer.schedule(new RemindTask(), 3000,3000);
}
}
答案 8 :(得分:2)
这是在java中使用线程的简单方法:
for(int i = 0; i< 10; i++) {
try {
//sending the actual Thread of execution to sleep X milliseconds
Thread.sleep(3000);
} catch(Exception e) {
System.out.println("Exception : "+e.getMessage());
}
System.out.println("Hello world!");
}
答案 9 :(得分:1)
他说的话。您可以随意处理异常,但是 了Thread.sleep(毫秒); 是最好的选择。
public static void main(String[] args) throws InterruptedException {
答案 10 :(得分:1)
这是在Thread Constructor中使用Runnable接口的另一种简单方法
public class Demo {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 5; i++){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread T1 : "+i);
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 5; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread T2 : "+i);
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 5; i++){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread T3 : "+i);
}
}
});
t1.start();
t2.start();
t3.start();
}
}
答案 11 :(得分:-1)
添加Thread.sleep
try {
Thread.sleep(3000);
} catch(InterruptedException ie) {}
答案 12 :(得分:-1)
对于小型应用程序,可以像Rohit一样使用Timer和TimerTask,但在Web应用程序中,我会使用Quartz Scheduler来安排作业并执行此类定期作业。
请参阅tutorials了解Quartz调度。
答案 13 :(得分:-1)
public class TimeDelay{
public static void main(String args[]) {
try {
while (true) {
System.out.println(new String("Hello world"));
Thread.sleep(3 * 1000); // every 3 seconds
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}