Java 如何将线程挂起呢?
下文笔者讲述线程挂起的方法分享,如下所示:
实现思路:
使用sleep方法即可将线程挂起
例:
public class SleepingThread extends Thread {
private int countDown = 3;
private static int threadCount = 0;
public SleepingThread() {
super("" + ++threadCount);
start();
}
public String toString() {
return "#" + getName() + ": " + countDown;
}
public void run() {
while (true) {
System.out.println(this);
if (--countDown == 0)
return;
try {
sleep(100);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args)
throws InterruptedException {
for (int i = 0; i < 5; i++){
new SleepingThread().join();
}
System.out.println("线程已被挂起");
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


