欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解三種java實(shí)現(xiàn)多線程的方式

 更新時(shí)間:2015年08月04日 08:16:24   作者:korelAK  
數(shù)據(jù)時(shí)代的到來,多線程一直都是比較關(guān)心的問題之一,這篇文章介紹了JAVA實(shí)現(xiàn)多線程的三種方法,有需要的朋友可以參考一下

java中實(shí)現(xiàn)多線程的方法有兩種:繼承Thread類和實(shí)現(xiàn)runnable接口。

1.繼承Thread類,重寫父類run()方法

 public class thread1 extends Thread {
 
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println("我是線程"+this.getId());
        }
    }
 
    public static void main(String[] args) {
        thread1 th1 = new thread1();
        thread1 th2 = new thread1();
        th1.run();
        th2.run();
    }
   }

run()方法只是普通的方法,是順序執(zhí)行的,即th1.run()執(zhí)行完成后才執(zhí)行th2.run(),這樣寫只用一個(gè)主線程。多線程就失去了意義,所以應(yīng)該用start()方法來啟動(dòng)線程,start()方法會(huì)自動(dòng)調(diào)用run()方法。上述代碼改為:

 public class thread1 extends Thread {
     
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println("我是線程"+this.getId());
        }
    }
 
    public static void main(String[] args) {
        thread1 th1 = new thread1();
        thread1 th2 = new thread1();
        th1.start();
        th2.start();
    }
}

通過start()方法啟動(dòng)一個(gè)新的線程。這樣不管th1.start()調(diào)用的run()方法是否執(zhí)行完,都繼續(xù)執(zhí)行th2.start()如果下面有別的代碼也同樣不需要等待th2.start()執(zhí)行完成,而繼續(xù)執(zhí)行。(輸出的線程id是無規(guī)則交替輸出的)

2.實(shí)現(xiàn)runnable接口

public class thread2 implements Runnable {
 
    public String ThreadName;
     
    public thread2(String tName){
        ThreadName = tName;
    }
     
     
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(ThreadName);
        }
    }
     
    public static void main(String[] args) {
        thread2 th1 = new thread2("線程A");
        thread2 th2 = new thread2("線程B");
        th1.run();
        th2.run();
    }
}


和Thread的run方法一樣Runnable的run只是普通方法,在main方法中th2.run()必須等待th1.run()執(zhí)行完成后才能執(zhí)行,程序只用一個(gè)線程。要多線程的目的,也要通過Thread的start()方法(注:runnable是沒有start方法)。上述代碼修改為:

public class thread2 implements Runnable {
 
    public String ThreadName;
     
    public thread2(String tName){
        ThreadName = tName;
    }
     
     
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(ThreadName);
        }
    }
     
    public static void main(String[] args) {
        thread2 th1 = new thread2("線程A");
        thread2 th2 = new thread2("Thread-B");
        Thread myth1 = new Thread(th1);
        Thread myth2 = new Thread(th2);
        myth1.start();
        myth2.start();
    }
}


3.使用ExecutorService、Callable、Future實(shí)現(xiàn)有返回結(jié)果的多線程(JDK5.0以后)
可返回值的任務(wù)必須實(shí)現(xiàn)Callable接口,類似的,無返回值的任務(wù)必須Runnable接口。執(zhí)行Callable任務(wù)后,可以獲取一個(gè)Future的對(duì)象,在該對(duì)象上調(diào)用get就可以獲取到Callable任務(wù)返回的Object了,再結(jié)合線程池接口ExecutorService就可以實(shí)現(xiàn)傳說中有返回結(jié)果的多線程了。下面提供了一個(gè)完整的有返回結(jié)果的多線程測(cè)試?yán)樱贘DK1.5下驗(yàn)證過沒問題可以直接使用。代碼如下:

import java.util.concurrent.*; 
import java.util.Date; 
import java.util.List; 
import java.util.ArrayList; 
  
/** 
* 有返回值的線程 
*/ 
@SuppressWarnings("unchecked") 
public class Test { 
public static void main(String[] args) throws ExecutionException, 
  InterruptedException { 
  System.out.println("----程序開始運(yùn)行----"); 
  Date date1 = new Date(); 
  
  int taskSize = 5; 
  // 創(chuàng)建一個(gè)線程池 
  ExecutorService pool = Executors.newFixedThreadPool(taskSize); 
  // 創(chuàng)建多個(gè)有返回值的任務(wù) 
  List<Future> list = new ArrayList<Future>(); 
  for (int i = 0; i < taskSize; i++) { 
  Callable c = new MyCallable(i + " "); 
  // 執(zhí)行任務(wù)并獲取Future對(duì)象 
  Future f = pool.submit(c); 
  // System.out.println(">>>" + f.get().toString()); 
  list.add(f); 
  } 
  // 關(guān)閉線程池 
  pool.shutdown(); 
  
  // 獲取所有并發(fā)任務(wù)的運(yùn)行結(jié)果 
  for (Future f : list) { 
  // 從Future對(duì)象上獲取任務(wù)的返回值,并輸出到控制臺(tái) 
  System.out.println(">>>" + f.get().toString()); 
  } 
  
  Date date2 = new Date(); 
  System.out.println("----程序結(jié)束運(yùn)行----,程序運(yùn)行時(shí)間【" 
   + (date2.getTime() - date1.getTime()) + "毫秒】"); 
} 
} 
  
class MyCallable implements Callable<Object> { 
private String taskNum; 
  
MyCallable(String taskNum) { 
  this.taskNum = taskNum; 
} 
  
public Object call() throws Exception { 
  System.out.println(">>>" + taskNum + "任務(wù)啟動(dòng)"); 
  Date dateTmp1 = new Date(); 
  Thread.sleep(1000); 
  Date dateTmp2 = new Date(); 
  long time = dateTmp2.getTime() - dateTmp1.getTime(); 
  System.out.println(">>>" + taskNum + "任務(wù)終止"); 
  return taskNum + "任務(wù)返回運(yùn)行結(jié)果,當(dāng)前任務(wù)時(shí)間【" + time + "毫秒】"; 
} 
}

代碼說明:
上述代碼中Executors類,提供了一系列工廠方法用于創(chuàng)先線程池,返回的線程池都實(shí)現(xiàn)了ExecutorService接口。
public static ExecutorService newFixedThreadPool(int nThreads)
創(chuàng)建固定數(shù)目線程的線程池。
public static ExecutorService newCachedThreadPool()
創(chuàng)建一個(gè)可緩存的線程池,調(diào)用execute 將重用以前構(gòu)造的線程(如果線程可用)。如果現(xiàn)有線程沒有可用的,則創(chuàng)建一個(gè)新線程并添加到池中。終止并從緩存中移除那些已有 60 秒鐘未被使用的線程。
public static ExecutorService newSingleThreadExecutor()
創(chuàng)建一個(gè)單線程化的Executor。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
創(chuàng)建一個(gè)支持定時(shí)及周期性的任務(wù)執(zhí)行的線程池,多數(shù)情況下可用來替代Timer類。
ExecutoreService提供了submit()方法,傳遞一個(gè)Callable,或Runnable,返回Future。如果Executor后臺(tái)線程池還沒有完成Callable的計(jì)算,這調(diào)用返回Future對(duì)象的get()方法,會(huì)阻塞直到計(jì)算完成。

總結(jié):實(shí)現(xiàn)java多線程的2種方式,runable是接口,thread是類,runnable只提供一個(gè)run方法,建議使用runable實(shí)現(xiàn) java多線程,不管如何,最終都需要通過thread.start()來使線程處于可運(yùn)行狀態(tài)。第三種方法是聽群里的兄弟們介紹的,所以就百度補(bǔ)上了。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • Java替換中使用正則表達(dá)式實(shí)現(xiàn)中間模糊匹配的方法

    Java替換中使用正則表達(dá)式實(shí)現(xiàn)中間模糊匹配的方法

    今天小編就為大家分享一篇Java替換中使用正則表達(dá)式實(shí)現(xiàn)中間模糊匹配的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 淺談Java slf4j日志簡單理解

    淺談Java slf4j日志簡單理解

    這篇文章主要介紹了淺談Java日志簡單理解,詳細(xì)的介紹了slf4j的概念和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Java包裝類的概述與應(yīng)用

    Java包裝類的概述與應(yīng)用

    包裝類使用起來非常方便,但是沒有對(duì)應(yīng)的方法來操作這些基本數(shù)據(jù)類型,可以使用一個(gè)類,把基本類型的數(shù)據(jù)裝起來,在類中定義一些方法,我們可以使用類中的方法來操作這些基本類型的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Java包裝類的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Java設(shè)計(jì)模式之原型模式的示例詳解

    Java設(shè)計(jì)模式之原型模式的示例詳解

    原型模式(Prototype Pattern)指使用原型實(shí)例指定創(chuàng)建對(duì)象的種類,并且通過拷貝這些原型創(chuàng)建新的對(duì)象。本文將通過案例詳細(xì)講解一下原型模式,感興趣的可以了解一下
    2022-02-02
  • ChatGPT介紹及Java?API調(diào)用

    ChatGPT介紹及Java?API調(diào)用

    本文主要介紹了ChatGPT介紹及Java?API調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 淺談在Spring中如何使用數(shù)據(jù)源(DBCP、C3P0、JNDI)

    淺談在Spring中如何使用數(shù)據(jù)源(DBCP、C3P0、JNDI)

    這篇文章主要介紹了淺談在Spring中如何使用數(shù)據(jù)源(DBCP、C3P0、JNDI),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • Spring框架中Bean的三種配置和實(shí)例化方法總結(jié)

    Spring框架中Bean的三種配置和實(shí)例化方法總結(jié)

    在Spring框架中,Bean的配置和實(shí)例化是很重要的基礎(chǔ)內(nèi)容,掌握各種配置方式,才能靈活管理Bean對(duì)象,本文將全面介紹Bean的別名配置、作用范圍配置,以及構(gòu)造器實(shí)例化、工廠實(shí)例化等方式
    2023-10-10
  • 簡單易懂的Java Map數(shù)據(jù)添加指南

    簡單易懂的Java Map數(shù)據(jù)添加指南

    Java提供了多種方法來往Map中添加數(shù)據(jù),開發(fā)者可以根據(jù)具體需求選擇合適的方法,需要的朋友可以參考下
    2023-11-11
  • Java中數(shù)組的創(chuàng)建與傳參方法(學(xué)習(xí)小結(jié))

    Java中數(shù)組的創(chuàng)建與傳參方法(學(xué)習(xí)小結(jié))

    這篇文章主要介紹了Java中數(shù)組的創(chuàng)建與傳參方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • Java實(shí)現(xiàn)考試系統(tǒng)

    Java實(shí)現(xiàn)考試系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)考試系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09

最新評(píng)論