色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

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

1、繼承Thread類方式

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

舉例:一個用于累加數(shù)組內數(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; }}

調用方式:

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

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

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

4、線程池方式

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

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

在創(chuàng)建線程池時,最好傳入 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)建線程的四種方式的詳細內容,更多關于java 創(chuàng)建線程的資料請關注好吧啦網(wǎng)其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 精品国产一区二区三区久久影院 | 欧美日韩亚洲综合另类ac | 欧美国产日本 | 欧美jizzhd精品欧美另类 | 国产精品99r8在线观看 | 鸥美性生交xxxxx久久久 | 久久91精品综合国产首页 | 久久99热只有视精品6国产 | 精品视频h | 亚洲综合色视频在线观看 | 精品国产免费一区二区三区五区 | 国产一区二区福利久久 | 综合在线亚洲 | 性刺激欧美三级在线现看中文 | 国产一级做a爰片久久毛片男 | 午夜在线视频一区二区三区 | 日韩在线观看一区二区三区 | 亚洲国产在 | 一级欧美 | 自拍 欧美 在线 综合 另类 | 一区二区国产在线观看 | 国产网站精品 | 欧美一级大片在线观看 | a级片在线观看视频 | 国产三级a三级三级三级 | 成人网18免费软件大全 | 欧美在线一区二区 | 亚洲精品久久一区二区无卡 | 国产三级在线观看播放 | 亚洲国产tv | 久在线观看 | 久久99精品免费视频 | 欧美另类孕交免费观看 | 精品国产欧美一区二区最新 | 欧美成人综合在线观看视频 | 美女黄色网页 | 日本三级毛片 | 国产精品v在线播放观看 | 精品亚洲欧美高清不卡高清 | 中文字幕亚洲高清综合 | 久久精品免看国产 |