国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java創建線程三種方式的優缺點

Java創建線程三種方式的優缺點

2021-06-21 13:43灰灰是菇涼呀 Java教程

今天小編就為大家分享一篇關于Java創建線程三種方式的優缺點,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

java創建線程主要有三種方式:繼承thread類創建線程、實現runnable接口創建線程和實現callable和future創建線程。

繼承thread類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class thread1 extends thread {
  @override
  public void run() {
    for (int i = 0; i < 10; i++) {
      system.out.println(getname() + ": " + i);
    }
  }
  public static void main(string[] args) {
    for (int i = 0; i < 10; i++) {
      system.out.println(thread.currentthread().getname() + ": " + i);
      if (i == 2) {
        new thread1().start();
        new thread1().start();
      }
    }
  }
}

實現runnable接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class thread2 implements runnable {
  @override
  public void run() {
    for (int i = 0; i < 10; i++) {
      system.out.println(thread.currentthread().getname() + ": " + i);
    }
  }
  public static void main(string[] args) {
    for (int i = 0; i < 10; i++) {
      system.out.println(thread.currentthread().getname() + ": " + i);
      if (i == 2) {
        thread2 thread2 = new thread2();
        new thread(thread2).start();
        new thread(thread2).start();
      }
    }
  }
}

實現callable接口

futuretask類包裝callable對象時,封裝了callable對象的call()方法的返回值。

?
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
class thread3 implements callable {
  @override
  public integer call() throws exception {
    int i = 0;
    for (; i < 10; i++) {
      system.out.println(thread.currentthread().getname() + ": " + i);
    }
    return i;
  }
  public static void main(string[] args) {
    thread3 thread3 = new thread3();
    futuretask<integer> futuretask = new futuretask<integer>(thread3);
    for (int i = 0; i < 10; i++) {
      system.out.println(thread.currentthread().getname() + " :" + i);
      if (i == 2) {
        new thread(futuretask, "有返回值的線程").start();
      }
    }
    try {
      system.out.println("子線程返回值: " + futuretask.get());
    } catch (interruptedexception e) {
      e.printstacktrace();
    } catch (executionexception e) {
      e.printstacktrace();
    }
  }
}

三種方式優缺點

采用實現接口(runnable和callable)的方式,線程類還可以繼承其他的類。實現接口的線程對象還可以用來創建多個線程,可以實現資源共享。缺點是不能使用this指針來獲取線程的名字等。

采用繼承thread類的方式,線程不能繼承其他的類,但是thread類中有getname方法,因為可以直接使用this.getname()來獲取當前線程的名字。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:https://blog.csdn.net/sinat_28394909/article/details/84956184

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 一级录像免费录像在线观看 | 国产毛片视频 | 欧美日韩亚洲二区 | 九九热视频精品在线观看 | 久久国产精品久久精品 | 成人免费观看高清视频 | 99精品一区 | 久久亚 | 综合久久久久 | 久久久久久久国产精品 | 欧美日韩免费视频 | 精品久久久久久久中文字幕 | 久久综合区 | 青青伊人久久 | 人人干日日干 | 成年人免费网站 | 中文字幕在线观看av | 精品视频网站 | 精精国产 | 国产精品久久99 | 欧美日韩一区二区三区在线观看 | 午夜视频在线播放 | 羞羞的视频在线 | 久久成人精品 | 天天精品 | 精品一二区 | 欧美日韩国产一区二区三区 | 亚洲一区av | 国产精品久久a | 国产综合亚洲精品一区二 | 婷婷午夜激情网 | 日本精品久久 | 精品国产视频 | 久久久久久久久久久蜜桃 | 国产精品久久久久aaaa九色 | 日本一区二区精品 | 中文字幕一区二区三区日韩精品 | 国产毛片欧美毛片久久久 | 亚洲国产精品久久久久婷婷老年 | 午夜欧美 | 日韩成人影院 |