最近我一直在使用循环具有大量打印出来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秒了吗?
Answer 1:
您还可以看看Timer
和TimerTask
,你可以用它来安排你的任务运行的每类n
秒。
你需要扩展一个类TimerTask
和覆盖public void run()
方法,这将在每次执行你传递的类的实例来timer.schedule()
方法..
下面是一个例子,它打印Hello World
每5秒: -
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);
Answer 2:
如果你想做一个周期性的任务,使用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);
Answer 3:
试着这样做:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Hello World");
}
}, 0, 5000);
该代码将运行打印到控制台的Hello World每5000毫秒(5秒)。 欲了解更多信息,请阅读https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html
Answer 4:
我弄明白了定时器,希望它帮助。 我用从定时器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);
Answer 5:
使用了Thread.sleep(3000)内为德路
Answer 6:
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());
}
}
}
}
无限循环创建广告调度任务的配置。
Answer 7:
最简单的方法是设置主线程休眠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
文档:
使当前执行的线程休眠(暂停执行)为指定的毫秒数, 受到的精度和系统计时器和调度程序的准确性 。
Answer 8:
使用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);
}
}
Answer 9:
这是使用线程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!");
}
Answer 10:
他说什么。 您可以处理异常,只要你喜欢,但的Thread.sleep(毫秒); 是采取的最佳路线。
public static void main(String[] args) throws InterruptedException {
Answer 11:
下面是使用线程构造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();
}
}
Answer 12:
添加Thread.sleep
try {
Thread.sleep(3000);
} catch(InterruptedException ie) {}
Answer 13:
对于小型应用来说是好的使用Timer和TimerTask作为罗希特提到,但在Web应用程序,我会用Quartz调度安排作业,并执行这样的周期性工作。
请参见教程为石英调度。
Answer 14:
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();
}
}
}
文章来源: Print “hello world” every X seconds