1. 程式人生 > 實用技巧 >Java 執行緒的生命週期完整例項分析

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: SyncThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am Sync Thread: SyncThread-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程式設計有所幫助。