ホームページ  >  記事  >  Java  >  Java の例 - スレッドがハングする

Java の例 - スレッドがハングする

黄舟
黄舟オリジナル
2016-12-27 13:36:581803ブラウズ

次の例は、スレッドを一時停止する方法を示しています:

/*
 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 の例です - スレッド一時停止の内容の詳細については、PHP に注意してください。中国語のウェブサイト (www.php.cn)!


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。