最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Java編程中線程的掛起、恢復(fù)和終止的方法

 更新時間:2015年09月29日 16:28:13   投稿:goldensun  
這篇文章主要介紹了詳解Java編程中線程的掛起、恢復(fù)和終止的方法,線程是Java學(xué)習(xí)中的重點(diǎn)和難點(diǎn)知識,需要的朋友可以參考下

有時,線程的掛起是很有用的。例如,一個獨(dú)立的線程可以用來顯示當(dāng)日的時間。如果用戶不希望用時鐘,線程被掛起。在任何情形下,掛起線程是很簡單的,一旦掛起,重新啟動線程也是一件簡單的事。

掛起,終止和恢復(fù)線程機(jī)制在Java 2和早期版本中有所不同。盡管你運(yùn)用Java 2的途徑編寫代碼,你仍需了解這些操作在早期Java環(huán)境下是如何完成的。例如,你也許需要更新或維護(hù)老的代碼。你也需要了解為什么Java 2會有這樣的變化。因?yàn)檫@些原因,下面內(nèi)容描述了執(zhí)行線程控制的原始方法,接著是Java 2的方法。
Java 1.1或更早版本的線程的掛起、恢復(fù)和終止

先于Java2的版本,程序用Thread 定義的suspend() 和 resume() 來暫停和再啟動線程。它們的形式如下:

  final void suspend( )
  final void resume( )


下面的程序描述了這些方法:

// Using suspend() and resume().
class NewThread implements Runnable {
  String name; // name of thread
  Thread t;
  NewThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start(); // Start the thread
  }
  // This is the entry point for thread.
  public void run() {
    try {
      for(int i = 15; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(200);
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
}
class SuspendResume {
  public static void main(String args[]) {
    NewThread ob1 = new NewThread("One");
    NewThread ob2 = new NewThread("Two");
    try {
      Thread.sleep(1000);
      ob1.t.suspend();
      System.out.println("Suspending thread One");
      Thread.sleep(1000);
      ob1.t.resume();
      System.out.println("Resuming thread One");
      ob2.t.suspend();
      System.out.println("Suspending thread Two");
      Thread.sleep(1000);
      ob2.t.resume();
      System.out.println("Resuming thread Two");
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }
    // wait for threads to finish
    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }
    System.out.println("Main thread exiting.");
  }
}

程序的部分輸出如下:

New thread: Thread[One,5,main]
One: 15
New thread: Thread[Two,5,main]
Two: 15
One: 14
Two: 14
One: 13
Two: 13
One: 12
Two: 12
One: 11
Two: 11
Suspending thread One
Two: 10
Two: 9
Two: 8
Two: 7
Two: 6
Resuming thread One
Suspending thread Two
One: 10
One: 9
One: 8
One: 7
One: 6
Resuming thread Two
Waiting for threads to finish.
Two: 5
One: 5
Two: 4
One: 4
Two: 3
One: 3
Two: 2
One: 2
Two: 1
One: 1
Two exiting.
One exiting.
Main thread exiting.

Thread類同樣定義了stop() 來終止線程。它的形式如下:

  void stop( )


一旦線程被終止,它不能被resume() 恢復(fù)繼續(xù)運(yùn)行。
Java中掛起、恢復(fù)和終止線程

Thread定義的suspend(),resume()和stop()方法看起來是管理線程的完美的和方便的方法,它們不能用于新Java版本的程序。下面是其中的原因。Thread類的suspend()方法在Java2中不被贊成,因?yàn)閟uspend()有時會造成嚴(yán)重的系統(tǒng)故障。假定對關(guān)鍵的數(shù)據(jù)結(jié)構(gòu)的一個線程被鎖定的情況,如果該線程在那里掛起,這些鎖定的線程并沒有放棄對資源的控制。其他的等待這些資源的線程可能死鎖。

Resume()方法同樣不被贊同。它不引起問題,但不能離開suspend()方法而獨(dú)立使用。Thread類的stop()方法同樣在Java 2中受到反對。這是因?yàn)樵摲椒赡軐?dǎo)致嚴(yán)重的系統(tǒng)故障。設(shè)想一個線程正在寫一個精密的重要的數(shù)據(jù)結(jié)構(gòu)且僅完成一個零頭。如果該線程在此刻終止,則數(shù)據(jù)結(jié)構(gòu)可能會停留在崩潰狀態(tài)。

因?yàn)樵贘ava 2中不能使用suspend(),resume()和stop() 方法來控制線程,你也許會想那就沒有辦法來停止,恢復(fù)和結(jié)束線程。其實(shí)不然。相反,線程必須被設(shè)計以使run() 方法定期檢查以來判定線程是否應(yīng)該被掛起,恢復(fù)或終止它自己的執(zhí)行。有代表性的,這由建立一個指示線程狀態(tài)的標(biāo)志變量來完成。只要該標(biāo)志設(shè)為“running”,run()方法必須繼續(xù)讓線程執(zhí)行。如果標(biāo)志為“suspend”,線程必須暫停。若設(shè)為“stop”,線程必須終止。

當(dāng)然,編寫這樣的代碼有很多方法,但中心主題對所有的程序應(yīng)該是相同的。

下面的例題闡述了從Object繼承的wait()和notify()方法怎樣控制線程的執(zhí)行。該例與前面講過的程序很像。然而,不被贊同的方法都沒有用到。讓我們思考程序的執(zhí)行。

NewTread 類包含了用來控制線程執(zhí)行的布爾型的實(shí)例變量suspendFlag。它被構(gòu)造函數(shù)初始化為false。Run()方法包含一個監(jiān)測suspendFlag 的同步聲明的塊。如果變量是true,wait()方法被調(diào)用以掛起線程。Mysuspend()方法設(shè)置suspendFlag為true。Myresume()方法設(shè)置suspendFlag為false并且調(diào)用notify()方法來喚起線程。最后,main()方法被修改以調(diào)用mysuspend()和myresume()方法。

// Suspending and resuming a thread for Java2
class NewThread implements Runnable {
  String name; // name of thread
  Thread t;
  boolean suspendFlag;
  NewThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    suspendFlag = false;
    t.start(); // Start the thread
  }
  // This is the entry point for thread.
  public void run() {
    try {
      for(int i = 15; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(200);
        synchronized(this) {
          while(suspendFlag) {
            wait();
          }
        }
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
  void mysuspend() {
    suspendFlag = true;
  }
  synchronized void myresume() {
    suspendFlag = false;
    notify();
  }
}
class SuspendResume {
  public static void main(String args[]) {
    NewThread ob1 = new NewThread("One");
    NewThread ob2 = new NewThread("Two");
    try {
     Thread.sleep(1000);
     ob1.mysuspend();
     System.out.println("Suspending thread One");
     Thread.sleep(1000);
     ob1.myresume();
     System.out.println("Resuming thread One");
     ob2.mysuspend();
     System.out.println("Suspending thread Two");
     Thread.sleep(1000);
     ob2.myresume();
     System.out.println("Resuming thread Two");
    } catch (InterruptedException e) {
     System.out.println("Main thread Interrupted");
    }
    // wait for threads to finish
    try {
     System.out.println("Waiting for threads to finish.");
     ob1.t.join();
     ob2.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }
    System.out.println("Main thread exiting.");
  }
}

該程序的輸出與前面的程序相同。此書的后面部分,你將看到用Java 2機(jī)制控制線程的更多例子。盡管這種機(jī)制不像老方法那樣“干凈”,然而,它是確保運(yùn)行時不發(fā)生錯誤的方法。它是所有新的代碼必須采用的方法。

相關(guān)文章

  • Intellij無法創(chuàng)建java文件解決方案

    Intellij無法創(chuàng)建java文件解決方案

    這篇文章主要介紹了Intellij無法創(chuàng)建java文件解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Mybatis中@Param注解的用法詳解

    Mybatis中@Param注解的用法詳解

    @Param注解的作用是給參數(shù)命名,參數(shù)命名后就能根據(jù)名字得到參數(shù)值,正確的將參數(shù)傳入sql語句中,下面這篇文章主要給大家介紹了關(guān)于Mybatis中@Param注解用法的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • 淺談xml配置spring profiles的幾個注意點(diǎn)

    淺談xml配置spring profiles的幾個注意點(diǎn)

    這篇文章主要介紹了淺談xml配置spring profiles的幾個注意點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • IDEA中項(xiàng)目集成git提交代碼的詳細(xì)步驟

    IDEA中項(xiàng)目集成git提交代碼的詳細(xì)步驟

    這篇文章主要介紹了IDEA中項(xiàng)目集成git提交代碼的詳細(xì)步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 圖解如何在Spring Boot中使用JSP頁面

    圖解如何在Spring Boot中使用JSP頁面

    這篇文章主要介紹了圖解如何在Spring Boot中使用JSP頁面,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • IDEA 非常重要的一些設(shè)置項(xiàng)(一連串的問題差點(diǎn)讓我重新用回 Eclipse)

    IDEA 非常重要的一些設(shè)置項(xiàng)(一連串的問題差點(diǎn)讓我重新用回 Eclipse)

    這篇文章主要介紹了IDEA 非常重要的一些設(shè)置項(xiàng)(一連串的問題差點(diǎn)讓我重新用回 Eclipse),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 一行java代碼實(shí)現(xiàn)高斯模糊效果

    一行java代碼實(shí)現(xiàn)高斯模糊效果

    這篇文章主要為大家詳細(xì)介紹了一行java代碼實(shí)現(xiàn)高斯模糊效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 如何處理器攔截器(HandlerInterceptor)

    如何處理器攔截器(HandlerInterceptor)

    這篇文章主要介紹了如何處理器攔截器(HandlerInterceptor)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Spark Streaming編程初級實(shí)踐詳解

    Spark Streaming編程初級實(shí)踐詳解

    這篇文章主要為大家介紹了Spark Streaming編程初級實(shí)踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • SpringBoot中condition注解的使用方式

    SpringBoot中condition注解的使用方式

    這篇文章主要介紹了SpringBoot中condition注解的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論

海口市| 上犹县| 措美县| 中牟县| 宜兰市| 吉林市| 呈贡县| 阜南县| 通州区| 惠水县| 陆川县| 扎鲁特旗| 新民市| 龙门县| 淮安市| 九龙县| 乌拉特中旗| 丰都县| 武功县| 广水市| 青州市| 绥芬河市| 绥阳县| 文山县| 临朐县| 聂拉木县| 迭部县| 沅陵县| 兴宁市| 芮城县| 周口市| 长宁区| 梁河县| 翁牛特旗| 合水县| 宁化县| 和林格尔县| 息烽县| 南陵县| 安多县| 三穗县|