以下实例演示了如何将线程挂起:
/*
author by w3cschool.cc
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 实例 - 线程挂起的内容。