一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 舉例講解Java中的多線程編程

舉例講解Java中的多線程編程

2020-01-07 14:21goldensun JAVA教程

這篇文章主要介紹了舉例講解Java中的多線程編程,線程是Java學習中的重要知識,需要的朋友可以參考下

Java創建線程(Runnable接口和Thread類)
大多數情況,通過實例化一個Thread對象來創建一個線程。Java定義了兩種方式:

  1. 實現Runnable 接口;
  2. 可以繼承Thread類。

下面的依次介紹了每一種方式。
實現Runnable接口

創建線程的最簡單的方法就是創建一個實現Runnable 接口的類。Runnable抽象了一個執行代碼單元。你可以通過實現Runnable接口的方法創建每一個對象的線程。為實現Runnable 接口,一個類僅需實現一個run()的簡單方法,該方法聲明如下:

?
1
public void run( )


在run()中可以定義代碼來構建新的線程。理解下面內容是至關重要的:run()方法能夠像主線程那樣調用其他方法,引用其他類,聲明變量。僅有的不同是run()在程序中確立另一個并發的線程執行入口。當run()返回時,該線程結束。

在你已經創建了實現Runnable接口的類以后,你要在類內部實例化一個Thread類的對象。Thread 類定義了好幾種構造函數。我們會用到的如下:

?
1
Thread(Runnable threadOb, String threadName)


該構造函數中,threadOb是一個實現Runnable接口類的實例。這定義了線程執行的起點。新線程的名稱由threadName定義。

建立新的線程后,它并不運行直到調用了它的start()方法,該方法在Thread 類中定義。本質上,start() 執行的是一個對run()的調用。 Start()方法聲明如下:

?
1
void start( )

下面的例子是創建一個新的線程并啟動它運行:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Create a second thread.
class NewThread implements Runnable {
  Thread t;
  NewThread() {
    // Create a new, second thread
    t = new Thread(this, "Demo Thread");
    System.out.println("Child thread: " + t);
    t.start(); // Start the thread
  }
 
  // This is the entry point for the second thread.
  public void run() {
    try {
      for(int i = 5; i > 0; i--) {
        System.out.println("Child Thread: " + i);
        Thread.sleep(500);
      }
    } catch (InterruptedException e) {
      System.out.println("Child interrupted.");
    }
    System.out.println("Exiting child thread.");
  }
}
 
class ThreadDemo {
  public static void main(String args[]) {
    new NewThread(); // create a new thread
    try {
      for(int i = 5; i > 0; i--) {
        System.out.println("Main Thread: " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted.");
    }
    System.out.println("Main thread exiting.");
  }
}

在NewThread 構造函數中,新的Thread對象由下面的語句創建::

?
1
t = new Thread(this, "Demo Thread");


通過前面的語句this 表明在this對象中你想要新的線程調用run()方法。然后,start() 被調用,以run()方法為開始啟動了線程的執行。這使子線程for 循環開始執行。調用start()之后,NewThread 的構造函數返回到main()。當主線程被恢復,它到達for 循環。兩個線程繼續運行,共享CPU,直到它們的循環結束。該程序的輸出如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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.

如前面提到的,在多線程程序中,通常主線程必須是結束運行的最后一個線程。實際上,一些老的JVM,如果主線程先于子線程結束,Java的運行時間系統就可能“掛起”。前述程序保證了主線程最后結束,因為主線程沉睡周期1000毫秒,而子線程僅為500毫秒。這就使子線程在主線程結束之前先結束。簡而言之,你將看到等待線程結束的更好途徑。
擴展Thread

創建線程的另一個途徑是創建一個新類來擴展Thread類,然后創建該類的實例。當一個類繼承Thread時,它必須重載run()方法,這個run()方法是新線程的入口。它也必須調用start()方法去啟動新線程執行。下面用擴展thread類重寫前面的程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Create a second thread by extending Thread
class NewThread extends Thread {
  NewThread() {
    // Create a new, second thread
    super("Demo Thread");
    System.out.println("Child thread: " + this);
    start(); // Start the thread
  }
 
  // This is the entry point for the second thread.
  public void run() {
    try {
      for(int i = 5; i > 0; i--) {
        System.out.println("Child Thread: " + i);
        Thread.sleep(500);
      }
    } catch (InterruptedException e) {
      System.out.println("Child interrupted.");
    }
    System.out.println("Exiting child thread.");
  }
}
 
class ExtendThread {
  public static void main(String args[]) {
    new NewThread(); // create a new thread
    try {
      for(int i = 5; i > 0; i--) {
        System.out.println("Main Thread: " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted.");
    }
    System.out.println("Main thread exiting.");
  }
}

該程序生成和前述版本相同的輸出。子線程是由實例化NewThread對象生成的,該對象從Thread類派生。注意NewThread 中super()的調用。該方法調用了下列形式的Thread構造函數:

?
1
public Thread(String threadName)


這里,threadName指定線程名稱。
選擇合適方法

到這里,你一定會奇怪為什么Java有兩種創建子線程的方法,哪一種更好呢。所有的問題都歸于一點。Thread類定義了多種方法可以被派生類重載。對于所有的方法,惟一的必須被重載的是run()方法。這當然是實現Runnable接口所需的同樣的方法。很多Java程序員認為類僅在它們被加強或修改時應該被擴展。因此,如果你不重載Thread的其他方法時,最好只實現Runnable 接口。這當然由你決定。然而,在本章的其他部分,我們應用實現runnable接口的類來創建線程。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人免费观看bbb | 青草草产国视频 | 国产日韩精品一区二区三区 | 亚洲成年人在线观看 | free service性v极品 | 男人操女人动图 | 天堂网在线.www天堂在线视频 | 天天爽视频 | 国产日韩欧美不卡www | 9久热这里只有精品视频在线观看 | 91人成尤物在线 | 久久电影院久久国产 | 青青草精品在线 | 日本xxxxx69hd日本 | 色综合精品 | 天天视频国产精品 | 视频一区二区三区在线观看 | 色帝国亚洲欧美在线蜜汁tv | java hd国产高清 | 91会员 | 欧美一区二区三区免费不卡 | 91在线播| 武侠古典久久亚洲精品 | 吉泽明步高清无码中文 | 日韩一本在线 | 日本乱人伦中文在线播放 | 99视频免费在线观看 | 多人群p全肉小说 | 极品丝袜老师h系列全文阅读 | 日韩去日本高清在线 | 国产精品理论片 | 99久久一香蕉国产线看观看 | chinese男gay飞机同志 | 日本一在线中文字幕天堂 | 午夜欧美精品久久久久久久 | 91综合精品网站久久 | 亚洲欧美精品久久 | 国产精品夜夜爽张柏芝 | 高清在线观看mv的网址免费 | 2019理论韩国理论中文 | 91久久精品国产亚洲 |