tes*_*est 1 java multithreading
假设我有这段代码:
public class helloworld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
Run Code Online (Sandbox Code Playgroud)
使用线程,有没有办法让我的Hello世界每5秒连续回声一次?
此版本连续重复hello world消息,同时允许用户终止消息编写线程:
public class HelloWorld {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(5000);
System.out.println("Hello World!");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
thread.start();
System.out.println("press any key to quit");
System.in.read();
thread.interrupt();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
997 次 |
| 最近记录: |