ToB企服应用市场:ToB评测及商务社交产业平台
标题:
Java 如何将线程挂起呢?
[打印本页]
作者:
泉缘泉
时间:
2022-8-29 13:16
标题:
Java 如何将线程挂起呢?
转自:
http://www.java265.com/JavaCourse/202204/3185.html
多线程:
多线程(multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理或同时多线程处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”
下文笔者讲述线程挂起的方法分享,如下所示:
<strong>实现思路:
使用sleep方法即可将线程挂起
</strong>
复制代码
例:
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("线程已被挂起");
}
}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4