Javaスレッドのライフサイクルの完全な実例分析


本論文の実例は、Javaスレッドのライフサイクルを説明する。皆さんに参考にしてあげます。具体的には以下の通りです。
一つのコード

/**
* @Title: ThreadStatus.java
* @Description: TODO(         )
*/
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.TimeUnit;
public class ThreadStatus
{
  private static Lock lock = new ReentrantLock();
  public static void main(String[] args)
  {
    new Thread(new TimeWaiting(), "TimeWaitingThread").start();
    new Thread(new Waiting(), "WaitingThread").start();
    //     Blocked  ,     ,     
    new Thread(new Blocked(), "BThread-1").start();
    new Thread(new Blocked(), "BThread-2").start();
    new Thread(new Sync(), "SyncThread-1").start();
    new Thread(new Sync(), "SyncThread-2").start();
  }
  //          
  static class TimeWaiting implements Runnable
  {
    public void run() {
      while (true)
      {
        try {
          TimeUnit.SECONDS.sleep(5);
          System.out.println("I am TimeWaiting Thread: "+ Thread.currentThread().getName());
        } catch (InterruptedException e) { }
      }
    }
  }
  //    Waiting.class     
  static class Waiting implements Runnable
  {
    public void run( )
    {
      while (true)
      {
        synchronized (Waiting.class)
        {
          try {
            System.out.println("I am Waiting Thread: "+ Thread.currentThread().getName());
            Waiting.class.wait( );
          } catch (InterruptedException e) {
            e.printStackTrace( );
          }
        }
      }
    }
  }
  //    Blocked.class      ,      
  static class Blocked implements Runnable
  {
    public void run( )
    {
      synchronized (Blocked.class)
      {
        while (true)
        {
          try {
            System.out.println("I am Blocked Thread: "+ Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(5);
          } catch (InterruptedException e) {}
        }
      }
    }
  }
  //        
  static class Sync implements Runnable
  {
    public void run( ) {
      lock.lock( );
      try {
        System.out.println("I am Sync Thread: "+ Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(5);
      } catch (InterruptedException e) { }
      finally {
        lock.unlock();
      }
    }
  }
}

二運転
I am Waiting Thread:WaitingThread
I am Blocked Thread:BThread-1
I am Sync Thread:Sync Thread-1
I am TimeWaiting Thread:TimeWaitingThread
I am Blocked Thread:BThread-1
I am Sync Thread:Sync Thread-2
I am TimeWaiting Thread:TimeWaitingThread
I am Blocked Thread:BThread-1
I am TimeWaiting Thread:TimeWaitingThread
I am Blocked Thread:BThread-1
I am TimeWaiting Thread:TimeWaitingThread
I am Blocked Thread:BThread-1
I am TimeWaiting Thread:TimeWaitingThread
I am Blocked Thread:BThread-1
I am TimeWaiting Thread:TimeWaitingThread
I am Blocked Thread:BThread-1
I am TimeWaiting Thread:TimeWaitingThread
I am Blocked Thread:BThread-1
I am TimeWaiting Thread:TimeWaitingThread
I am Blocked Thread:BThread-1
もっと多くのjava関連の内容に興味がある読者は当駅のテーマを調べてもいいです。「Javaプロセスとスレッド操作テクニックのまとめ」、「Javaデータ構造とアルゴリズム教程」、「Java操作DOMノード技術のまとめ」、「Javaファイルとディレクトリの操作テクニックのまとめ」、「Javaキャッシュ操作テクニックのまとめ
本論文で述べたように、皆さんのjavaプログラムの設計に役に立ちます。