您当前的位置:首页 > IT编程 > Java
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch |

自学教程:Java 实例 - 线程挂起

51自学网 2023-01-08 20:13:32
  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 实例 - 获取线程id
Java 实例 - 终止线程
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1