最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 創(chuàng)建線程的四種方式

 更新時(shí)間:2020年11月13日 08:28:17   作者:(•̀ω•́)y  
這篇文章主要介紹了java 創(chuàng)建線程的四種方式,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下

1、繼承Thread類方式

這種方式適用于執(zhí)行特定任務(wù),并且需要獲取處理后的數(shù)據(jù)的場(chǎng)景。

舉例:一個(gè)用于累加數(shù)組內(nèi)數(shù)據(jù)的和的線程。

public class AdditionThread extends Thread {
 private int sum = 0;
 private int[] nums;
​
 public AdditionThread(int[] nums, String threadName) {
  super(threadName);
  this.nums = nums;
 }
​
 @Override
 public void run() {
  for (int num : nums) {
   sum += num;
  }
 }
​
 public int getSum() {
  return sum;
 }
}

調(diào)用方式:

public class Main {
 public static void main(String[] args) throws InterruptedException {
  int[] nums = {10, 12, 15, 200, 100};
  AdditionThread thread = new AdditionThread(nums, "AdditionThread");
  thread.start();
  thread.join();
​
  System.out.println("sum=" + thread.getSum());
 }
}

2、Runnable 接口方式

定義一個(gè)實(shí)現(xiàn)Runnable接口的類,或者直接創(chuàng)建一個(gè)匿名內(nèi)部類,并覆蓋 run() 方法。最后作為參數(shù)傳給Thread的構(gòu)造函數(shù)。

public class Main {
 public static void main(String[] args) {
  // 自定義的 Runnable
  Runnable runnable = new MyRunnable();
  Thread thread = new Thread(runnable, "Runnable-Thread");
  thread.start();
​
  // 自定義匿名內(nèi)部類
  new Thread(() -> {
   System.out.println("Inner class");
  }).start();
 }
​
 static class MyRunnable implements Runnable {
  @Override
  public void run() {
   System.out.println("MyRunnable");
  }
 }
}

3、 Callable 接口方式

Callable 接口與 Runnable 接口的區(qū)別:

(1)Callable 的方法為call(),Runnable的方法為run()。

(2)Callable 的方法由返回值,Runnable 沒有。

(3)Callable 的方法聲明的Exception,Runnable的沒有。

public class Main {
 public static void main(String[] args) {
  MyCallable myCallable = new MyCallable();
  FutureTask<String> task = new FutureTask<>(myCallable);
  Thread thread = new Thread(task, "FutureTask");
  thread.start();
  try {
   // 通過get方法獲取返回值
   String result = task.get();
   System.out.println(result);
  } catch (InterruptedException | ExecutionException e) {
   e.printStackTrace();
  }
 }
​
 static class MyCallable implements Callable<String> {
  @Override
  public String call() throws Exception {
   // 模擬超時(shí)操作
   Thread.sleep(1000);
   return "OK";
  }
 }
}

4、線程池方式

我們可以通過 ThreadPoolExecutor 類的構(gòu)造函數(shù)來創(chuàng)建線程池,也可以通過Executors工廠方法來創(chuàng)建,如

// 創(chuàng)建固定線程數(shù)的線程池
Executors.newFixedThreadPool(); 
// 創(chuàng)建只有一個(gè)核心線程的線程池
Executors.newSingleThreadExecutor();
// 創(chuàng)建一個(gè)沒有核心線程,但可以緩存線程的線程池
Executors.newCachedThreadPool();
// 創(chuàng)建一個(gè)適用于執(zhí)行定時(shí)任務(wù)的線程池
Executors.newScheduledThreadPool();

在創(chuàng)建線程池時(shí),最好傳入 ThreadFactory 參數(shù),指定線程池所創(chuàng)建線程的名稱。這樣有利于分析定位可能存在的問題。

public class Main {
 private static final ExecutorService SERVICE =
  Executors.newFixedThreadPool(5, new BasicThreadFactory("My-Thread"));
​
 public static void main(String[] args) {
  // 打印線程的名字
  System.out.println("main thread name:" + Thread.currentThread().getName());
  SERVICE.execute(() -> {
   System.out.println("Hello thread pool.");
   // 打印線程池里的線程的名字
   System.out.println("thread name:" + Thread.currentThread().getName());
  });
 }
​
 static class BasicThreadFactory implements ThreadFactory {
  private final AtomicInteger threadNumber = new AtomicInteger(0);
  private final String basicName;
​
  public BasicThreadFactory(String basicName) {
   this.basicName = basicName;
  }
​
  @Override
  public Thread newThread(Runnable runnable) {
   Thread thread = new Thread(runnable);
   String name = this.basicName + "-" + threadNumber.incrementAndGet();
   thread.setName(name);
   return thread;
  }
 }
}

以上就是java 創(chuàng)建線程的四種方式的詳細(xì)內(nèi)容,更多關(guān)于java 創(chuàng)建線程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java日期工具類實(shí)例分享

    java日期工具類實(shí)例分享

    本文介紹一個(gè)java日期工具類,功能有英文簡(jiǎn)寫、英文全稱、精確到毫秒的完整時(shí)間、中文簡(jiǎn)寫、中文全稱等方法
    2014-01-01
  • 快速入門HarmonyOS的Java UI框架的教程

    快速入門HarmonyOS的Java UI框架的教程

    這篇文章主要介紹了快速入門HarmonyOS的Java UI框架,本文給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • springboot加載配值文件的實(shí)現(xiàn)步驟

    springboot加載配值文件的實(shí)現(xiàn)步驟

    本文主要介紹了springboot加載配值文件的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • Trace?在多線程異步體系下傳遞流程解析

    Trace?在多線程異步體系下傳遞流程解析

    這篇文章主要為大家介紹了Trace?在多線程異步體系下傳遞流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java實(shí)現(xiàn)辦公文檔在線預(yù)覽功能

    Java實(shí)現(xiàn)辦公文檔在線預(yù)覽功能

    java實(shí)現(xiàn)辦公文件在線預(yù)覽功能是一個(gè)大家在工作中也許會(huì)遇到的需求,這篇文章就教大家如何實(shí)現(xiàn)這一功能,感興趣的小伙伴可以了解一下
    2021-12-12
  • Springboot集成RabbitMQ并驗(yàn)證五種消息模型

    Springboot集成RabbitMQ并驗(yàn)證五種消息模型

    RabbitMQ 提供了5種常用消息模型,本文主要介紹了Springboot集成RabbitMQ并驗(yàn)證五種消息模型,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • SpringIOC refresh()初始化代碼實(shí)例

    SpringIOC refresh()初始化代碼實(shí)例

    這篇文章主要介紹了SpringIOC refresh()初始化代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 一小時(shí)迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API

    一小時(shí)迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API

    這篇文章主要介紹了一小時(shí)迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • java實(shí)現(xiàn)導(dǎo)出Excel的功能

    java實(shí)現(xiàn)導(dǎo)出Excel的功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)導(dǎo)出Excel的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • springAOP的三種實(shí)現(xiàn)方式示例代碼

    springAOP的三種實(shí)現(xiàn)方式示例代碼

    這篇文章主要介紹了springAOP的三種實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07

最新評(píng)論

南投市| 漳平市| 商河县| 多伦县| 池州市| 琼海市| 平利县| 隆德县| 西藏| 抚宁县| 民和| 米脂县| 湟中县| 平利县| 湖北省| 华容县| 沂南县| 密山市| 大姚县| 兴宁市| 石泉县| 怀远县| 鹤岗市| 安顺市| 禹州市| 任丘市| 屯门区| 仪陇县| 连云港市| 彰化县| 肥城市| 手游| 岳普湖县| 柳州市| 左云县| 万山特区| 中阳县| 华宁县| 准格尔旗| 莱芜市| 昂仁县|