Java 实例 - 线程挂起

Java 实例 - 线程挂起

Java 实例 - 线程挂起

Java 实例

以下实例演示了如何将线程挂起:

SleepingThread.java 文件

public class SleepingThread extends Thread {

private int countDown = 5;

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("线程已被挂起");

}

}

以上代码运行输出结果为:

#1: 5

#1: 4

#1: 3

#1: 2

#1: 1

……

#5: 3

#5: 2

#5: 1

线程已被挂起

Java 实例

相关推荐

Mac 新增資料夾教學,3 種方法快速新增、刪除或整理資料夾
什么软件可以免费清理c盘,还能自动清理C盘垃圾?看这篇就够了
DNF要打新使徒了,那么来聊聊讨伐使徒的幕后黑手
完美体育365官方网站

DNF要打新使徒了,那么来聊聊讨伐使徒的幕后黑手

02-21 👁️ 7469
逆水寒点卡封顶机制
365bet体育在线中文

逆水寒点卡封顶机制

10-23 👁️ 5658
《鴡》字的近义词、《鴡》字的反义词大全
365bet体育在线中文

《鴡》字的近义词、《鴡》字的反义词大全

09-12 👁️ 3907
魔兽世界角色分离费用详解:值得吗?
完美体育365官方网站

魔兽世界角色分离费用详解:值得吗?

08-09 👁️ 2413
国足世预赛18强赛完整赛程公布!9月5日首战日本
梦幻手游多少级钓鱼(梦幻手游钓鱼收入图鉴)
365bet体育在线中文

梦幻手游多少级钓鱼(梦幻手游钓鱼收入图鉴)

01-21 👁️ 4328
甄秀梅医生治疗不孕不育的患者评价
完美体育365官方网站

甄秀梅医生治疗不孕不育的患者评价

09-29 👁️ 2022