ToB企服应用市场:ToB评测及商务社交产业平台

标题: Java 如何将线程挂起呢? [打印本页]

作者: 徐锦洪    时间: 2022-8-29 13:18
标题: Java 如何将线程挂起呢?
转自:
http://www.java265.com/JavaCourse/202204/3185.html
多线程:
      多线程(multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理或同时多线程处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”
 
下文笔者讲述线程挂起的方法分享,如下所示:
  1. <strong>实现思路:
  2.     使用sleep方法即可将线程挂起
  3. </strong>
复制代码
例:
  1. public class SleepingThread extends Thread {
  2.    private int countDown = 3;
  3.    private static int threadCount = 0;
  4.    public SleepingThread() {
  5.       super("" + ++threadCount);
  6.       start();
  7.    }
  8.    public String toString() {
  9.       return "#" + getName() + ": " + countDown;
  10.    }
  11.    public void run() {
  12.       while (true) {
  13.          System.out.println(this);
  14.          if (--countDown == 0)
  15.             return;
  16.          try {
  17.             sleep(100);
  18.          }
  19.          catch (InterruptedException e) {
  20.             throw new RuntimeException(e);
  21.          }
  22.       }
  23.    }
  24.    public static void main(String[] args)
  25.    throws InterruptedException {
  26.       for (int i = 0; i < 5; i++){
  27.             new SleepingThread().join();
  28.         }
  29.       System.out.println("线程已被挂起");
  30.    }
  31. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4