编程语言
首页 > 编程语言> > Java 如何将线程挂起呢?

Java 如何将线程挂起呢?

作者:互联网

转自:

http://www.java265.com/JavaCourse/202204/3185.html

多线程:

      多线程(multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理或同时多线程处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”


 

下文笔者讲述线程挂起的方法分享,如下所示:

实现思路:
    使用sleep方法即可将线程挂起

例:

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

标签:Java,int,countDown,如何,线程,SleepingThread,多线程,public
来源: https://www.cnblogs.com/java265/p/16467298.html