国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

多線程從線程繼承

2019-11-18 12:20:53
字體:
供稿:網(wǎng)友

  為創(chuàng)建一個(gè)線程,最簡(jiǎn)單的方法就是從Thread類繼續(xù)。這個(gè)類包含了創(chuàng)建和運(yùn)行線程所需的一切東西。Thread最重要的方法是run()。但為了使用run(),必須對(duì)其進(jìn)行過載或者覆蓋,使其能充分按自己的吩咐行事。因此,run()屬于那些會(huì)與程序中的其他線程“并發(fā)”或“同時(shí)”執(zhí)行的代碼。
  下面這個(gè)例子可創(chuàng)建任意數(shù)量的線程,并通過為每個(gè)線程分配一個(gè)獨(dú)一無二的編號(hào)(由一個(gè)靜態(tài)變量產(chǎn)生),從而對(duì)不同的線程進(jìn)行跟蹤。Thread的run()方法在這里得到了覆蓋,每通過一次循環(huán),計(jì)數(shù)就減1——計(jì)數(shù)為0時(shí)則完成循環(huán)(此時(shí)一旦返回run(),線程就中止運(yùn)行)。
  //: SimpleThread.java
  // Very simple Threading example
  public class SimpleThread extends Thread {
   PRivate int countDown = 5;
   private int threadNumber;
   private static int threadCount = 0;
   public SimpleThread() {
   threadNumber = ++threadCount;
   System.out.println("Making " + threadNumber);
   }
   public void run() {
   while(true) {
   System.out.println("Thread " +
   threadNumber + "(" + countDown + ")");
   if(--countDown == 0) return;
   }
   }
   public static void main(String[] args) {
   for(int i = 0; i < 5; i++)
   new SimpleThread().start();
   System.out.println("All Threads Started");
   }
  }
  run()方法幾乎肯定含有某種形式的循環(huán)——它們會(huì)一直持續(xù)到線程不再需要為止。因此,我們必須規(guī)定特定的條件,以便中斷并退出這個(gè)循環(huán)(或者在上述的例子中,簡(jiǎn)單地從run()返回即可)。run()通常采用一種無限循環(huán)的形式。也就是說,通過阻止外部發(fā)出對(duì)線程的stop()或者destroy()調(diào)用,它會(huì)永遠(yuǎn)運(yùn)行下去(直到程序完成)。
  在main()中,可看到創(chuàng)建并運(yùn)行了大量線程。Thread包含了一個(gè)非凡的方法,叫作start(),它的作用是對(duì)線程進(jìn)行非凡的初始化,然后調(diào)用run()。所以整個(gè)步驟包括:調(diào)用構(gòu)建器來構(gòu)建對(duì)象,然后用start()配置線程,再調(diào)用run()。假如不調(diào)用start()——假如適當(dāng)?shù)脑挘稍跇?gòu)建器那樣做——線程便永遠(yuǎn)不會(huì)啟動(dòng)。
  下面是該程序某一次運(yùn)行的輸出(注重每次運(yùn)行都會(huì)不同):
  Making 1
  Making 2
  Making 3
  Making 4
  Making 5
  Thread 1(5)
  Thread 1(4)
  Thread 1(3)
  Thread 1(2)
  Thread 2(5)
  Thread 2(4)
  Thread 2(3)
  Thread 2(2)
  Thread 2(1)
  Thread 1(1)
  All Threads Started
  Thread 3(5)
  Thread 4(5)
  Thread 4(4)
  Thread 4(3)
  Thread 4(2)
  Thread 4(1)
  Thread 5(5)
  Thread 5(4)
  Thread 5(3)
  Thread 5(2)
  Thread 5(1)
  Thread 3(4)
  Thread 3(3)
  Thread 3(2)
  Thread 3(1)
  可注重到這個(gè)例子中到處都調(diào)用了sleep(),然而輸出結(jié)果指出每個(gè)線程都獲得了屬于自己的那一部分CPU執(zhí)行時(shí)間。從中可以看出,盡管sleep()依靠一個(gè)線程的存在來執(zhí)行,但卻與答應(yīng)或禁止線程無關(guān)。它只不過是另一個(gè)不同的方法而已。
  亦可看出線程并不是按它們創(chuàng)建時(shí)的順序運(yùn)行的。事實(shí)上,CPU處理一個(gè)現(xiàn)有線程集的順序是不確定的——除非我們親自介入,并用Thread的setPriority()方法調(diào)整它們的優(yōu)先級(jí)。
  main()創(chuàng)建Thread對(duì)象時(shí),它并未捕捉任何一個(gè)對(duì)象的句柄。普通對(duì)象對(duì)于垃圾收集來說是一種“公平競(jìng)賽”,但線程卻并非如此。每個(gè)線程都會(huì)“注冊(cè)”自己,所以某處實(shí)際存在著對(duì)它的一個(gè)引用。這樣一來,垃圾收集器便只好對(duì)它“瞠目以對(duì)”了。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙雅县| 繁峙县| 屏东县| 岗巴县| 泽普县| 额济纳旗| 昭苏县| 永春县| 永春县| 福建省| 镇远县| 永昌县| 宁河县| 寻甸| 乌恰县| 嵩明县| 嘉鱼县| 青冈县| 福贡县| 江北区| 屏边| 南汇区| 阜新市| 义马市| 玉门市| 深州市| 宁乡县| 德钦县| 鹤庆县| 泾源县| 昌邑市| 防城港市| 武宁县| 高青县| 河南省| 射阳县| 托里县| 武鸣县| 屯留县| 舞钢市| 新晃|