亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

java 創建線程的四種方式

瀏覽:3日期:2022-08-21 10:37:22

1、繼承Thread類方式

這種方式適用于執行特定任務,并且需要獲取處理后的數據的場景。

舉例:一個用于累加數組內數據的和的線程。

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; }}

調用方式:

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 接口方式

定義一個實現Runnable接口的類,或者直接創建一個匿名內部類,并覆蓋 run() 方法。最后作為參數傳給Thread的構造函數。

public class Main { public static void main(String[] args) { // 自定義的 Runnable Runnable runnable = new MyRunnable(); Thread thread = new Thread(runnable, 'Runnable-Thread'); thread.start();​ // 自定義匿名內部類 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 接口的區別:

(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 { // 模擬超時操作 Thread.sleep(1000); return 'OK'; } }}

4、線程池方式

我們可以通過 ThreadPoolExecutor 類的構造函數來創建線程池,也可以通過Executors工廠方法來創建,如

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

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

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 創建線程的四種方式的詳細內容,更多關于java 創建線程的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 免费观看的毛片 | 免费无遮挡嘿嘿嘿视频动态 | 国内小情侣一二三区在线视频 | 国产日韩欧美综合一区二区三区 | 看黄色一级视频 | 国产主播一区二区 | 综合色图 | 精品国产一区二区三区久久影院 | 一级黄色影院 | 欧美日韩一区二区三区免费 | 国产aaa级一级毛片 国产aaa免费视频国产 | 全国男人的天堂网站 | 91短视频免费在线观看 | 任我鲁这里有精品视频在线播 | 国产精品看片 | 真人一级毛片免费完整视 | 国产欧美日韩不卡一区二区三区 | 人人狠狠综合久久亚洲 | 日韩一级在线播放 | 人做人爱全免费视频 | 国产乱辈通伦影片在线播放 | 一国产一级淫片a免费播放口 | 4388x17亚洲最大成人网 | 国产免费做爰午夜视频 | 国产原创剧情在线 | 一级毛片一级毛片一级毛片aa | 久久9966e这里只有精品 | 国产二区三区 | 91久久精品青青草原伊人 | 97视频在线免费观看 | 香蕉爱爱视频 | 国内精品视频九九九九 | 大香焦久久 | 亚洲精品不卡午夜精品 | 在线观看成年人免费视频 | 亚洲国产精品久久久久网站 | 无遮挡毛片 | 国产精品成人麻豆专区 | 亚洲国产天堂久久九九九 | 免费在线黄色片 | 国产永久地址 |