亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java  多線程的三種構(gòu)建方法

 更新時間:2017年09月17日 14:43:45   投稿:lqh  
這篇文章主要介紹了java 多線程的三種構(gòu)建方法的相關(guān)資料,這里提供三種實現(xiàn)方法,希望大家能夠掌握,很重要的基礎(chǔ)知識,需要的朋友可以參考下

java  多線程的三種構(gòu)建方法

繼承Thread類創(chuàng)建線程類

public class Thread extends Object implements Runnable
  1. 定義Thread類的子類,并重寫其run()方法
  2. 創(chuàng)建Thread子類的實例,即創(chuàng)建了線程對象
  3. 調(diào)用線程對象的start()方法啟動線程
public class FirstThread extends Thread {
  public void run(){
    for(int i=0;i<100;i++){
      /*
       * Thread類已經(jīng)繼承了Object
       * Object類創(chuàng)建了name選項 并且有其getName(),setName()方法
       * 在繼承Thread的類里面使用時只需要用this引用
      */
      System.out.println(this.getName()+" "+i);
    }
  }

  public static void main(String[] args) {
    for(int i=0;i<100;i++){
      System.out.println(Thread.currentThread().getName()+" "+i);
      if(i==20){
        new FirstThread().start();
        new FirstThread().start();
      }
    }
  }

}

Thread類已經(jīng)繼承了Object

Object類創(chuàng)建了name選項 并且有其getName(),setName()方法

在繼承Thread的類里面使用時只需要用this引用

上面兩個副線程和主線程隨機切換,又因為使用的是繼承Thread的類所以兩個副線程不能共享資源

start()方法調(diào)用后并不是立即執(zhí)行多線程代碼,而是使得該線程編程可運行狀態(tài),什么時候運行是由操作系統(tǒng)決定的

實現(xiàn)Runnable接口創(chuàng)建線程類

public Thread() 
public Thread(Runnable target) 
public Thread(Runnable target,String name)
  • 定義Runnable接口的實現(xiàn)類,并重寫該接口的run()方法
  • 創(chuàng)建Runnable實現(xiàn)類的實例,并以此作為Thread的target來創(chuàng)建Thread對象,該Thread對象才是真正的線程對象。
public class SecondThread implements Runnable {
  public void run(){
    for(int i=0;i<100;i++){
      System.out.println(Thread.currentThread().getName()+" "+i);
    }
  }

  public static void main(String[] args) {
    for(int i=0;i<100;i++){
      System.out.println(Thread.currentThread().getName()+" "+i);

      if(i==20){
        SecondThread st=new SecondThread();
        //通過new Thread(target,name)創(chuàng)建線程
        new Thread(st,"新線程1").start();
        new Thread(st,"新線程2").start();
      }
    }
  }
}

上面的結(jié)果是兩個副線程和主線程隨機切換,但是并沒有共享資源,因為他們根本沒有能用來共享的資源。

start()方法調(diào)用后并不是立即執(zhí)行多線程代碼,而是使得該線程編程可運行狀態(tài),什么時候運行是由操作系統(tǒng)決定的
繼承Thread類和創(chuàng)建Runnable接口的共享資源詳解

在只有可以用來共享的資源時候,也就是同用一個實例化對象。兩個創(chuàng)建方式在共享資源時才會有所區(qū)別,否則它們都不會共享資源共享資源通常用private static 修飾符來修飾。

class Thread1 extends Thread{ 
  private int count=5; 
  private String name; 
  public Thread1(String name) { 
    this.name=name; 
  } 
  public void run() { 
    for (int i = 0; i < 5; i++) { 
      System.out.println(name + "運行 count= " + count--); 
      try { 
        sleep((int) Math.random() * 10); 
      } catch (InterruptedException e) { 
        e.printStackTrace(); 
      } 
    } 

  } 
} 

public class Main { 

  public static void main(String[] args) { 
    Thread1 mTh1=new Thread1("A"); 
    Thread1 mTh2=new Thread1("B"); 
    mTh1.start(); 
    mTh2.start(); 

  } 

} 

B運行 count= 5 
A運行 count= 5 
B運行 count= 4 
B運行 count= 3 
B運行 count= 2 
B運行 count= 1 
A運行 count= 4 
A運行 count= 3 
A運行 count= 2 
A運行 count= 1

正是因為有了private int count=5;一句才有了共享資源,但這是繼承Thread類的子類,并不能共享資源

class Thread2 implements Runnable{ 
  private int count=15; 
  public void run() { 
     for (int i = 0; i < 5; i++) { 
       System.out.println(Thread.currentThread().getName() + "運行 count= " + count--); 
        try { 
          Thread.sleep((int) Math.random() * 10); 
        } catch (InterruptedException e) { 
          e.printStackTrace(); 
        } 
      } 

  } 

} 
public class Main { 

  public static void main(String[] args) { 

    Thread2 my = new Thread2(); 
      new Thread(my, "C").start();//同一個mt,但是在Thread中就不可以,如果用同一個實例化對象mt,就會出現(xiàn)異常   
      new Thread(my, "D").start(); 
      new Thread(my, "E").start(); 
  } 

} 

C運行 count= 15 
D運行 count= 14 
E運行 count= 13 
D運行 count= 12 
D運行 count= 10 
D運行 count= 9 
D運行 count= 8 
C運行 count= 11 
E運行 count= 12 
C運行 count= 7 
E運行 count= 6 
C運行 count= 5 
E運行 count= 4 
C運行 count= 3 
E運行 count= 2

同樣的正是因為有了private int count=15這個共同的實例化對象,實現(xiàn)Runnable的類才可以共享資源

那么為什么繼承Thread類的子類實現(xiàn)Runable接口的類在共享資源時有區(qū)別呢?

因為Java中只能支持單繼承,單繼承特點意味著只能有一個子類去繼承 而Runnabl接口后可以跟好多類,便可以進行多個線程共享一個資源的操作

使用Callable和Future創(chuàng)建線程

Callable怎么看起來都像Runnable接口的增強版,Callable有一個call()方法相當于Runnable的run()方法,但是功能卻更加強大:

call()方法可以有返回值
call()方法可以聲明拋出異常

Callable接口有泛型限制,Callable接口里的泛型形參類型與call()方法的返回值類型相同。 而且Callable接口是函數(shù)式接口,因此可使用Lambda表達式創(chuàng)建Callable對象 Runnable接口也是函數(shù)式接口,因此也可以使用Lambda表達式創(chuàng)建Runnable對象

  1. 創(chuàng)建Callable接口的實現(xiàn)類,并實現(xiàn)call()方法,該call()方法將作為線程執(zhí)行體,再創(chuàng)建Callable實現(xiàn)類的實例
  2. 使用FutureTask類來包裝Callable對象,該FutureTask對象封裝了該Callable對象的call()方法
  3. 使用FutureTask類對象作為Thread對象的target創(chuàng)建并啟動新線程
  4. 調(diào)用FutureTask對象的get()方法來獲得子線程結(jié)束后的返回值
public class ThirdThread implements Callable<Integer> {
  public Integer call(){
    int i=0;
    for(;i<100;i++){
      System.out.println(Thread.currentThread().getName()+" "+i);
    }
    return i;
  }

  public static void main(String[] args){
    ThirdThread tt=new ThirdThread();
    FutureTask<Integer> task=new FutureTask<>(tt);
    Thread t=new Thread(task,"有返回值的線程");
    for(int i=0;i<100;i++){
      System.out.println(Thread.currentThread().getName()+" "+i);
      if(i==20){
        t.start();
      }
    }
    try{
      System.out.println("返回值是:"+task.get());
    }catch(Exception e){
      e.printStackTrace();
    }
  }
}

使用Lambda表達式的Callable和Future創(chuàng)建的線程

public class ThirdThread{
  public static void main(String[] args){
    ThirdThread tt=new ThirdThread();
    //先使用Lambda表達式創(chuàng)建Callable<Integer>對象
    //使用FutureTask封裝Callable對象
    FutureTask<Integer> task=new FutureTask<Integer>((Callable<Integer>)()->{
      int i=0;
      for(;i<100;i++){
        System.out.println(Thread.currentThread().getName()+"的循環(huán)變量i的值:"+i);
      }
      return i;
    });

    for(int i=0;i<100;i++){
      System.out.println(Thread.currentThread().getName()+"的循環(huán)變量i的值:"+i);
      if(i==20){
        new Thread(task,"有返回值的線程").start();
      }
    }
    try{
      System.out.println("子線程的返回值"+task.get());
    }catch(Exception e){
      e.printStackTrace();
    }
  }
}

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論