【发布时间】:2016-09-30 09:14:19
【问题描述】:
import java.util.Date;
import javax.xml.crypto.Data;
public class Task1 {
public static void main(String[] args) {
// run in a second
final long timeInterval = 4000;
Data now = null;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
// ------- code for task to run
System.out.println("Hello !!"+new Date());
for(int i=0;i<10000;i++){
System.out.println("Hello !!");
}
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
我阅读了有关线程调度的教程。我想了解代码运行良好并在 4 秒后执行线程,但如果 for 循环所花费的时间假设为 1 秒,那么总时间将为 1+ 4 5 秒。 我不想执行服务。只是普通的线程类。谁能解释这个程序是如何工作的。
【问题讨论】:
标签: java multithreading thread-sleep