久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      java多線程的創(chuàng)建及啟動(dòng)

      java多線程的創(chuàng)建及啟動(dòng)

      Java中線程的創(chuàng)建常見有如三種基本形式:

      一、繼承Thread類,重寫該類的run()方法

      繼承Thread類,重寫該類的run()方法

      public class MyThread extends Thread{     @Override     public void run() {         for (int i = 0 ;i < 50;i++) {             System.out.println(Thread.currentThread().getName() + ":" + i);         }     }     public static void main(String[] args) {         for (int i = 0;i<50;i++) {             //調(diào)用Thread類的currentThread()方法獲取當(dāng)前線程             System.out.println(Thread.currentThread().getName() + " " + i);             if (i == 10) {                 new MyThread().start();                 new MyThread().start();              }         }     } }

      運(yùn)行結(jié)果:

      ... main 48 main 49 Thread-0:0 Thread-0:1 Thread-0:2 Thread-0:3 Thread-0:4 Thread-1:0 ...

      從結(jié)果中可以看出:

      1、有三個(gè)線程:main、Thread-0 、Thread-1;

      2、Thread-0 、Thread-1兩個(gè)線程輸出的成員變量 i 的值不連續(xù)(這里的 i 是實(shí)例變量而不是局部變量)。因?yàn)椋和ㄟ^繼承Thread類實(shí)現(xiàn)多線程時(shí),每個(gè)線程的創(chuàng)建都要?jiǎng)?chuàng)建不同的子類對(duì)象,導(dǎo)致Thread-0 、Thread-1兩個(gè)線程不能共享成員變量 i ;

      3、線程的執(zhí)行是搶占式,并沒有說Thread-0 或者Thread-1一直占用CPU(這也與線程優(yōu)先級(jí)有關(guān),這里Thread-0 、Thread-1線程優(yōu)先級(jí)相同,關(guān)于線程優(yōu)先級(jí)的知識(shí)這里不做展開)

      (學(xué)習(xí)視頻推薦:java視頻教程)

      二、通過實(shí)現(xiàn)Runnable接口創(chuàng)建線程類

      定義一個(gè)類實(shí)現(xiàn)Runnable接口;創(chuàng)建該類的實(shí)例對(duì)象obj;將obj作為構(gòu)造器參數(shù)傳入Thread類實(shí)例對(duì)象,這個(gè)對(duì)象才是真正的線程對(duì)象。

      public class MyRunnable implements Runnable {     @Override     public void run() {         for (int i = 0 ;i < 50 ;i++) {             System.out.println(Thread.currentThread().getName()+":" +i);         }     }      public static void main(String[] args) {        for (int i = 0;i < 50;i++) {             System.out.println(Thread.currentThread().getName() + ":" +i);             if (i == 10) {                 MyRunnable myRunnable = new MyRunnable();                 new Thread(myRunnable).start();                 new Thread(myRunnable).start();             }         }         //java8 labdam方式          new Thread(() -> {             System.out.println(Thread.currentThread().getName());         },"線程3").start();     } }

      運(yùn)行結(jié)果:

      ... main:46 main:47 main:48 main:49 Thread-0:28 Thread-0:29 Thread-0:30 Thread-1:30 ...

      1、線程1和線程2輸出的成員變量i是連續(xù)的,也就是說通過這種方式創(chuàng)建線程,可以使多線程共享線程類的實(shí)例變量,因?yàn)檫@里的多個(gè)線程都使用了同一個(gè)target實(shí)例變量。但是,當(dāng)你使用上述的代碼運(yùn)行的時(shí)候,你會(huì)發(fā)現(xiàn),其實(shí)結(jié)果有些并不連續(xù),這是因?yàn)槎鄠€(gè)線程訪問同一資源時(shí),如果資源沒有加鎖,那么會(huì)出現(xiàn)線程安全問題;

      2、java8 可以使用lambda方式創(chuàng)建多線程。

      三、通過Callable和Future接口創(chuàng)建線程

      創(chuàng)建Callable接口實(shí)現(xiàn)類,并實(shí)現(xiàn)call()方法,該方法將作為線程執(zhí)行體,且該方法有返回值,再創(chuàng)建Callable實(shí)現(xiàn)類的實(shí)例;使用FutureTask類來包裝Callable對(duì)象,該FutureTask對(duì)象封裝了該Callable對(duì)象的call()方法的返回值;使用FutureTask對(duì)象作為Thread對(duì)象的target創(chuàng)建并啟動(dòng)新線程;調(diào)用FutureTask對(duì)象的get()方法來獲得子線程執(zhí)行結(jié)束后的返回值。

      public class MyCallable implements Callable<Integer> {     private int i = 0;     @Override     public Integer call() throws Exception {         int sum = 0;         for (; i < 100; i++) {             System.out.println(Thread.currentThread().getName() + " " + i);             sum += i;         }         return sum;     }      public static void main(String[] args) throws ExecutionException, InterruptedException {         // 創(chuàng)建MyCallable對(duì)象         Callable<Integer> myCallable = new MyCallable();         //使用FutureTask來包裝MyCallable對(duì)象         FutureTask<Integer> ft = new FutureTask<Integer>(myCallable);         for (int i = 0;i<50;i++) {             System.out.println(Thread.currentThread().getName() + ":" + i);             if (i == 30) {                 Thread thread = new Thread(ft);                 thread.start();             }         }         System.out.println("主線程for循環(huán)執(zhí)行完畢..");         Integer integer = ft.get();         System.out.println("sum = "+ integer);     } }

      call()方法的返回值類型與創(chuàng)建FutureTask對(duì)象時(shí)<>里的類型一致。

      相關(guān)教程推薦:java快速入門

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)