国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

JAVA ???? ??? / Java 多線程編程

Java 多線程編程

Java給多線程編程提供了內(nèi)置的支持。一個多線程程序包含兩個或多個能并發(fā)運行的部分。程序的每一部分都稱作一個線程,并且每個線程定義了一個獨立的執(zhí)行路徑。

多線程是多任務的一種特別的形式,但多線程使用了更小的資源開銷。

這里定義和線程相關的另一個術語 - 進程:一個進程包括由操作系統(tǒng)分配的內(nèi)存空間,包含一個或多個線程。一個線程不能獨立的存在,它必須是進程的一部分。一個進程一直運行,直到所有的非守候線程都結束運行后才能結束。

多線程能滿足程序員編寫高效率的程序來達到充分利用CPU的目的。


一個線程的生命周

線程經(jīng)過其生命周期的各個階段。下圖顯示了一個線程完整的生命周期。

java-thread.jpg

  • 新建狀態(tài):

    使用 new 關鍵字和 Thread 類或其子類建立一個線程對象后,該線程對象就處于新建狀態(tài)。它保持這個狀態(tài)直到程序 start() 這個線程。

  • 就緒狀態(tài):

    當線程對象調用了start()方法之后,該線程就進入就緒狀態(tài)。就緒狀態(tài)的線程處于就緒隊列中,要等待JVM里線程調度器的調度。

  • 運行狀態(tài):

    如果就緒狀態(tài)的線程獲取 CPU 資源,就可以執(zhí)行 run(),此時線程便處于運行狀態(tài)。處于運行狀態(tài)的線程最為復雜,它可以變?yōu)樽枞麪顟B(tài)、就緒狀態(tài)和死亡狀態(tài)。

  • 阻塞狀態(tài):

    如果一個線程執(zhí)行了sleep(睡眠)、suspend(掛起)等方法,失去所占用資源之后,該線程就從運行狀態(tài)進入阻塞狀態(tài)。在睡眠時間已到或獲得設備資源后可以重新進入就緒狀態(tài)。

  • 死亡狀態(tài):

    一個運行狀態(tài)的線程完成任務或者其他終止條件發(fā)生時,該線程就切換到終止狀態(tài)。


線程的優(yōu)先級

每一個Java線程都有一個優(yōu)先級,這樣有助于操作系統(tǒng)確定線程的調度順序。

Java線程的優(yōu)先級是一個整數(shù),其取值范圍是1 (Thread.MIN_PRIORITY ) - 10 (Thread.MAX_PRIORITY )。

默認情況下,每一個線程都會分配一個優(yōu)先級NORM_PRIORITY(5)。

具有較高優(yōu)先級的線程對程序更重要,并且應該在低優(yōu)先級的線程之前分配處理器資源。但是,線程優(yōu)先級不能保證線程執(zhí)行的順序,而且非常依賴于平臺。


創(chuàng)建一個線程

Java提供了兩種創(chuàng)建線程方法:

  • 通過實現(xiàn)Runable接口;

  • 通過繼承Thread類本身。


通過實現(xiàn)Runnable接口來創(chuàng)建線程

創(chuàng)建一個線程,最簡單的方法是創(chuàng)建一個實現(xiàn)Runnable接口的類。

為了實現(xiàn)Runnable,一個類只需要執(zhí)行一個方法調用run(),聲明如下:


public void run()

你可以重寫該方法,重要的是理解的run()可以調用其他方法,使用其他類,并聲明變量,就像主線程一樣。

在創(chuàng)建一個實現(xiàn)Runnable接口的類之后,你可以在類中實例化一個線程對象。

Thread定義了幾個構造方法,下面的這個是我們經(jīng)常使用的:

Thread(Runnable threadOb,String threadName);

這里,threadOb 是一個實現(xiàn)Runnable 接口的類的實例,并且 threadName指定新線程的名字。

新線程創(chuàng)建之后,你調用它的start()方法它才會運行。

void start();

實例

下面是一個創(chuàng)建線程并開始讓它執(zhí)行的實例:

// 創(chuàng)建一個新的線程
class NewThread implements Runnable {
   Thread t;
   NewThread() {
      // 創(chuàng)建第二個新線程
      t = new Thread(this, "Demo Thread");
      System.out.println("Child thread: " + t);
      t.start(); // 開始線程
   }
  
   // 第二個線程入口
   public void run() {
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Child Thread: " + i);
            // 暫停線程
            Thread.sleep(50);
         }
     } catch (InterruptedException e) {
         System.out.println("Child interrupted.");
     }
     System.out.println("Exiting child thread.");
   }
}
 
public class ThreadDemo {
   public static void main(String args[]) {
      new NewThread(); // 創(chuàng)建一個新線程
      try {
         for(int i = 5; i > 0; i--) {
           System.out.println("Main Thread: " + i);
           Thread.sleep(100);
         }
      } catch (InterruptedException e) {
         System.out.println("Main thread interrupted.");
      }
      System.out.println("Main thread exiting.");
   }
}

編譯以上程序運行結果如下:

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

通過繼承Thread來創(chuàng)建線程

創(chuàng)建一個線程的第二種方法是創(chuàng)建一個新的類,該類繼承Thread類,然后創(chuàng)建一個該類的實例。

繼承類必須重寫run()方法,該方法是新線程的入口點。它也必須調用start()方法才能執(zhí)行。

實例

// 通過繼承 Thread 創(chuàng)建線程
class NewThread extends Thread {
   NewThread() {
      // 創(chuàng)建第二個新線程
      super("Demo Thread");
      System.out.println("Child thread: " + this);
      start(); // 開始線程
   }
 
   // 第二個線程入口
   public void run() {
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Child Thread: " + i);
                            // 讓線程休眠一會
            Thread.sleep(50);
         }
      } catch (InterruptedException e) {
         System.out.println("Child interrupted.");
      }
      System.out.println("Exiting child thread.");
   }
}
 
public class ExtendThread {
   public static void main(String args[]) {
      new NewThread(); // 創(chuàng)建一個新線程
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Main Thread: " + i);
            Thread.sleep(100);
         }
      } catch (InterruptedException e) {
         System.out.println("Main thread interrupted.");
      }
      System.out.println("Main thread exiting.");
   }
}

編譯以上程序運行結果如下:

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

Thread 方法

下表列出了Thread類的一些重要方法:

序號方法描述
                    1public void start()
使該線程開始執(zhí)行;Java 虛擬機調用該線程的 run 方法。
                    2public void run()
如果該線程是使用獨立的 Runnable 運行對象構造的,則調用該 Runnable 對象的 run 方法;否則,該方法不執(zhí)行任何操作并返回。
                    3public final void setName(String name)
改變線程名稱,使之與參數(shù) name 相同。
                    4public final void setPriority(int priority)
 更改線程的優(yōu)先級。
                    5public final void setDaemon(boolean on)
將該線程標記為守護線程或用戶線程。
                    6public final void join(long millisec)
等待該線程終止的時間最長為 millis 毫秒。
                    7public void interrupt()
中斷線程。
                    8public final boolean isAlive()
測試線程是否處于活動狀態(tài)。

測試線程是否處于活動狀態(tài)。 上述方法是被Thread對象調用的。下面的方法是Thread類的靜態(tài)方法。

序號方法描述
                    1public static void yield()
暫停當前正在執(zhí)行的線程對象,并執(zhí)行其他線程。
                    2public static void sleep(long millisec)
在指定的毫秒數(shù)內(nèi)讓當前正在執(zhí)行的線程休眠(暫停執(zhí)行),此操作受到系統(tǒng)計時器和調度程序精度和準確性的影響。
                    3public static boolean holdsLock(Object x)
當且僅當當前線程在指定的對象上保持監(jiān)視器鎖時,才返回 true。
                    4public static Thread currentThread()
返回對當前正在執(zhí)行的線程對象的引用。
                    5public static void dumpStack()
將當前線程的堆棧跟蹤打印至標準錯誤流。

實例

如下的ThreadClassDemo 程序演示了Thread類的一些方法:

// 文件名 : DisplayMessage.java
// 通過實現(xiàn) Runnable 接口創(chuàng)建線程
public class DisplayMessage implements Runnable
{
   private String message;
   public DisplayMessage(String message)
   {
      this.message = message;
   }
   public void run()
   {
      while(true)
      {
         System.out.println(message);
      }
   }
}
// 文件名 : GuessANumber.java
// 通過繼承 Thread 類創(chuàng)建線程

public class GuessANumber extends Thread
{
   private int number;
   public GuessANumber(int number)
   {
      this.number = number;
   }
   public void run()
   {
      int counter = 0;
      int guess = 0;
      do
      {
          guess = (int) (Math.random() * 100 + 1);
          System.out.println(this.getName()
                       + " guesses " + guess);
          counter++;
      }while(guess != number);
      System.out.println("** Correct! " + this.getName()
                       + " in " + counter + " guesses.**");
   }
}
// 文件名 : ThreadClassDemo.java
public class ThreadClassDemo
{
   public static void main(String [] args)
   {
      Runnable hello = new DisplayMessage("Hello");
      Thread thread1 = new Thread(hello);
      thread1.setDaemon(true);
      thread1.setName("hello");
      System.out.println("Starting hello thread...");
      thread1.start();
     
      Runnable bye = new DisplayMessage("Goodbye");
      Thread thread2 = new Thread(bye);
      thread2.setPriority(Thread.MIN_PRIORITY);
      thread2.setDaemon(true);
      System.out.println("Starting goodbye thread...");
      thread2.start();
 
      System.out.println("Starting thread3...");
      Thread thread3 = new GuessANumber(27);
      thread3.start();
      try
      {
         thread3.join();
      }catch(InterruptedException e)
      {
         System.out.println("Thread interrupted.");
      }
      System.out.println("Starting thread4...");
      Thread thread4 = new GuessANumber(75);
     
           thread4.start();
      System.out.println("main() is ending...");
   }
}

運行結果如下,每一次運行的結果都不一樣。

Starting hello thread...
Starting goodbye thread...
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Thread-2 guesses 27
Hello
** Correct! Thread-2 in 102 guesses.**
Hello
Starting thread4...
Hello
Hello
..........remaining result produced.

線程的幾個主要概念:

在多線程編程時,你需要了解以下幾個概念:

  • 線程同步

  • 線程間通信

  • 線程死鎖

  • 線程控制:掛起、停止和恢復


多線程的使用

有效利用多線程的關鍵是理解程序是并發(fā)執(zhí)行而不是串行執(zhí)行的。例如:程序中有兩個子系統(tǒng)需要并發(fā)執(zhí)行,這時候就需要利用多線程編程。

通過對多線程的使用,可以編寫出非常高效的程序。不過請注意,如果你創(chuàng)建太多的線程,程序執(zhí)行的效率實際上是降低了,而不是提升了。

請記住,上下文的切換開銷也很重要,如果你創(chuàng)建了太多的線程,CPU花費在上下文的切換的時間將多于執(zhí)行程序的時間!