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

詳解Java線程池的使用(7種創(chuàng)建方法)

 更新時間:2023年03月24日 11:08:11   作者:Youcan.  
這篇文章主要介紹了詳解Java線程池的使用(7種創(chuàng)建方法),線程池的創(chuàng)建?式總共包含7種,其中6種是通過Executors創(chuàng)建的,1種是通過ThreadPoolExecutor創(chuàng)建的,今天我們就來詳細說一下

線程池的創(chuàng)建?法總共有 7 種,但總體來說可分為 2 類: 

1. 通過 ThreadPoolExecutor 創(chuàng)建的線程池;        

2. 通過 Executors 創(chuàng)建的線程池。

線程池的創(chuàng)建?式總共包含以下 7 種(其中 6 種是通過 Executors 創(chuàng)建的, 1 種是通過 ThreadPoolExecutor 創(chuàng)建的):

1. Executors.newFixedThreadPool:創(chuàng)建?個固定??的線程池,可控制并發(fā)的線程數(shù),超出的線程會在隊列中等待;         
2. Executors.newCachedThreadPool:創(chuàng)建?個可緩存的線程池,若線程數(shù)超過處理所需,緩存?段時間后會回收,若線程數(shù)不夠,則新建線程;        
 3. Executors.newSingleThreadExecutor:創(chuàng)建單個線程數(shù)的線程池,它可以保證先進先出的執(zhí)?順序;         
4. Executors.newScheduledThreadPool:創(chuàng)建?個可以執(zhí)?延遲任務(wù)的線程池;         
5. Executors.newSingleThreadScheduledExecutor:創(chuàng)建?個單線程的可以執(zhí)?延遲任務(wù)的線程池;
6. Executors.newWorkStealingPool:創(chuàng)建?個搶占式執(zhí)?的線程池(任務(wù)執(zhí)?順序不確定)【JDK1.8 添加】。
 7. ThreadPoolExecutor:最原始的創(chuàng)建線程池的?式,它包含了 7 個參數(shù)可供設(shè)置,后?會詳細講。

 1. 固定數(shù)量的線程池

public class ThreadPoolDemo3 {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(2);
        //添加任務(wù)方式 1
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });
 
        //添加任務(wù)方式2
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });
    }
}
輸出:
pool-1-thread-1
pool-1-thread-2

a.  線程池返回結(jié)果

public class ThreadPoolDemo4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService threadPool =  Executors.newFixedThreadPool(2);
        //執(zhí)行任務(wù)
        Future<Integer> result = threadPool.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int num = new Random().nextInt(10);
                System.out.println("隨機數(shù)" + num);
                return num;
            }
        });
 
        //打印線程池返回方式
        System.out.println("返回結(jié)果:" + result.get());
    }
}
輸出
隨機數(shù)8
返回結(jié)果:8

使用submit可以執(zhí)行有返回值的任務(wù)或者是無返回值的任務(wù);而execute只能執(zhí)行不帶返回值的任務(wù)。 

b. ?定義線程池名稱或優(yōu)先級

public class ThreadPoolDemo5 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
         // 創(chuàng)建線程工廠
        ThreadFactory threadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                //?。。。。。?!一定要注意:要把任務(wù)Runnable設(shè)置給新創(chuàng)建的線程
                Thread thread = new Thread(r);
                //設(shè)置線程的命名規(guī)則
                thread.setName("我的線程" + r.hashCode());
                //設(shè)置線程的優(yōu)先級
                thread.setPriority(Thread.MAX_PRIORITY);
                return thread;
            }
        };
        ExecutorService threadPool = Executors.newFixedThreadPool(2,threadFactory);
        //執(zhí)行任務(wù)1
        Future<Integer> result = threadPool.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int num = new Random().nextInt(10);
                System.out.println(Thread.currentThread().getPriority() + ", 隨機數(shù):" + num);
                return num;
            }
        });
        //打印線程池返回結(jié)果
        System.out.println("返回結(jié)果:" + result.get());
    }
}

 提供的功能:

        1. 設(shè)置(線程池中)線程的命名規(guī)則。

        2. 設(shè)置線程的優(yōu)先級。

        3. 設(shè)置線程分組。

        4. 設(shè)置線程類型(用戶線程、守護線程)。

2. 帶緩存的線程池

public class ThreadPoolDemo6 {
    public static void main(String[] args) {
        //創(chuàng)建線程池
        ExecutorService service = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            service.submit(() -> {
                System.out.println("i : " + finalI + "|線程名稱:" + Thread.currentThread().getName());
            });
        }
    }
}
輸出
i : 1|線程名稱:pool-1-thread-2
i : 4|線程名稱:pool-1-thread-5
i : 3|線程名稱:pool-1-thread-4
i : 5|線程名稱:pool-1-thread-6
i : 0|線程名稱:pool-1-thread-1
i : 2|線程名稱:pool-1-thread-3
i : 6|線程名稱:pool-1-thread-7
i : 7|線程名稱:pool-1-thread-8
i : 8|線程名稱:pool-1-thread-9
i : 9|線程名稱:pool-1-thread-1

優(yōu)點:線程池會根據(jù)任務(wù)數(shù)量創(chuàng)建線程池,并且在一定時間內(nèi)可以重復使用這些線程,產(chǎn)生相應的線程池。

缺點:適用于短時間有大量任務(wù)的場景,它的缺點是可能會占用很多的資源。

3. 執(zhí)?定時任務(wù) a. 延遲執(zhí)?(?次)

public class ThreadPoolDemo7 {
    public static void main(String[] args) {
        //創(chuàng)建線程池
        ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
        System.out.println("添加任務(wù)的時間:" + LocalDateTime.now());
        //執(zhí)行定時任務(wù)(延遲3s執(zhí)行)只執(zhí)行一次
        service.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("執(zhí)行子任務(wù):" + LocalDateTime.now());
            }
        },3, TimeUnit.SECONDS);
    }
}
輸出
添加任務(wù)的時間:2022-04-13T14:19:39.983
執(zhí)行子任務(wù):2022-04-13T14:19:42.987

  b. 固定頻率執(zhí)?

public class ThreadPoolDemo8 {
    public static void main(String[] args) {
        //創(chuàng)建線程池
        ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
        System.out.println("添加任務(wù)時間:" + LocalDateTime.now());
        //2s之后開始執(zhí)行定時任務(wù),定時任務(wù)每隔4s執(zhí)行一次
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("執(zhí)行任務(wù):" + LocalDateTime.now());
            }
        },2,4, TimeUnit.SECONDS);
    }
}
輸出
添加任務(wù)時間:2022-04-13T14:24:38.810
執(zhí)行任務(wù):2022-04-13T14:24:40.814
執(zhí)行任務(wù):2022-04-13T14:24:44.814
執(zhí)行任務(wù):2022-04-13T14:24:48.813
執(zhí)行任務(wù):2022-04-13T14:24:52.815
執(zhí)行任務(wù):2022-04-13T14:24:56.813
執(zhí)行任務(wù):2022-04-13T14:25:00.813
執(zhí)行任務(wù):2022-04-13T14:25:04.814
執(zhí)行任務(wù):2022-04-13T14:25:08.813
... ...
... ...
執(zhí)行任務(wù):2022-04-13T14:26:44.814
執(zhí)行任務(wù):2022-04-13T14:26:48.813

 注意事項:

public class ThreadPoolDemo9 {
    public static void main(String[] args) {
        //創(chuàng)建線程池
        ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
        System.out.println("添加任務(wù)時間:" + LocalDateTime.now());
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("執(zhí)行任務(wù): " + LocalDateTime.now());
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },2,4, TimeUnit.SECONDS);
    }
}
輸出
添加任務(wù)時間:2022-04-13T14:33:34.551
執(zhí)行任務(wù): 2022-04-13T14:33:36.556
執(zhí)行任務(wù): 2022-04-13T14:33:41.557
執(zhí)行任務(wù): 2022-04-13T14:33:46.559
執(zhí)行任務(wù): 2022-04-13T14:33:51.561
執(zhí)行任務(wù): 2022-04-13T14:33:56.562
執(zhí)行任務(wù): 2022-04-13T14:34:01.564
執(zhí)行任務(wù): 2022-04-13T14:34:06.566
執(zhí)行任務(wù): 2022-04-13T14:34:11.566
執(zhí)行任務(wù): 2022-04-13T14:34:16.567
執(zhí)行任務(wù): 2022-04-13T14:34:21.570
執(zhí)行任務(wù): 2022-04-13T14:34:26.570
... ....

c. scheduleAtFixedRate VS scheduleWithFixedDelay

scheduleAtFixedRate 是以上?次任務(wù)的開始時間,作為下次定時任務(wù)的參考時間的(參考時間+延遲任務(wù)=任務(wù)執(zhí)?)。 scheduleWithFixedDelay 是以上?次任務(wù)的結(jié)束時間,作為下次定時任務(wù)的參考時間的。

public class ThreadPoolDemo10 {
    public static void main(String[] args) {
        //創(chuàng)建線程池
        ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
        System.out.println("添加任務(wù)時間:" + LocalDateTime.now());
        //2s之后開始執(zhí)行定時任務(wù),定時任務(wù)每隔4s執(zhí)行一次
        service.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("執(zhí)行任務(wù):" + LocalDateTime.now());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 2, 4, TimeUnit.SECONDS);
    }
}
輸出
添加任務(wù)時間:2022-04-13T14:46:02.871
執(zhí)行任務(wù):2022-04-13T14:46:04.876
執(zhí)行任務(wù):2022-04-13T14:46:09.878
執(zhí)行任務(wù):2022-04-13T14:46:14.880
執(zhí)行任務(wù):2022-04-13T14:46:19.883
執(zhí)行任務(wù):2022-04-13T14:46:24.885
執(zhí)行任務(wù):2022-04-13T14:46:29.888
執(zhí)行任務(wù):2022-04-13T14:46:34.888
執(zhí)行任務(wù):2022-04-13T14:46:39.891
執(zhí)行任務(wù):2022-04-13T14:46:44.893
執(zhí)行任務(wù):2022-04-13T14:46:49.895
執(zhí)行任務(wù):2022-04-13T14:46:54.897
執(zhí)行任務(wù):2022-04-13T14:46:59.900
執(zhí)行任務(wù):2022-04-13T14:47:04.901
... ...

4. 定時任務(wù)單線程

public class ThreadPoolDemo11 {
    public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        System.out.println("添加任務(wù)的時間:" + LocalDateTime.now());
        service.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("執(zhí)行時間:" + LocalDateTime.now());
            }
        },2, TimeUnit.SECONDS );
    }
}
輸出
添加任務(wù)的時間:2022-04-13T15:06:38.100
執(zhí)行時間:2022-04-13T15:06:40.106

5. 單線程線程池

public class ThreadPoolDemo12 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newSingleThreadScheduledExecutor();
        for (int i = 0; i < 10; i++) {
            service.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("線程名:" + Thread.currentThread().getName());
                }
            });
        }
    }
}
輸出
線程名:pool-1-thread-1
線程名:pool-1-thread-1
線程名:pool-1-thread-1
線程名:pool-1-thread-1
線程名:pool-1-thread-1
線程名:pool-1-thread-1
線程名:pool-1-thread-1
線程名:pool-1-thread-1
線程名:pool-1-thread-1
線程名:pool-1-thread-1

(MS) 為什么不直接用線程?

單線程的線程池又什么意義?

        1. 復用線程。

        2. 單線程的線程池提供了任務(wù)隊列和拒絕策略(當任務(wù)隊列滿了之后(Integer.MAX_VALUE),新來的任務(wù)就會拒絕策略)

6. 根據(jù)當前CPU?成線程池

public class ThreadPoolDemo13 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newWorkStealingPool();
        for (int i = 0; i < 10; i++) {
            service.submit(() -> {
                System.out.println("線程名" + Thread.currentThread().getName());
            });
            
            while(!service.isTerminated()) {
            }
        }
    }
}
輸出
線程名ForkJoinPool-1-worker-1

7. ThreadPoolExecutor

(1). Executors ?動創(chuàng)建線程池可能存在的問題

a. OOM 代碼演示

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class ThreadPoolDemo54 {
    static class MyOOMClass {
        // 1M 空間(M KB Byte)
        private byte[] bytes = new byte[1 * 1024 * 1024];
   }
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(15 * 1000);
        ExecutorService service = Executors.newCachedThreadPool();
        Object[] objects = new Object[15];
        for (int i = 0; i < 15; i++) {
            final int finalI = i;
            service.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(finalI * 200);
                   } catch (InterruptedException e) {
                        e.printStackTrace();
                   }
                    MyOOMClass myOOMClass = new MyOOMClass();
                    objects[finalI] = myOOMClass;
                    System.out.println("任務(wù):" + finalI);
               }
           });
       }
   }
}

 執(zhí)行結(jié)果:

b. 關(guān)于參數(shù)設(shè)置

-XX:標準設(shè)置,所有 HotSpot 都?持的參數(shù)。 -X:?標準設(shè)置,特定的 HotSpot 才?持的參數(shù)。 -D:程序參數(shù)設(shè)置,-D參數(shù)=value,程序中使?:System.getProperty("獲取")。

mx 是 memory max 的簡稱 

(2).  ThreadPoolExecutor 使?

a. ThreadPoolExecutor 參數(shù)說明

b. 線程池執(zhí)?流程

c. 執(zhí)?流程驗證

 <MS>

線程池的重要執(zhí)行節(jié)點:

1. 當任務(wù)來了之后,判斷線程池中實際線程數(shù)是否小于核心線程數(shù),如果小于就直接創(chuàng)建并執(zhí)行任務(wù)。

2. 當實際線程數(shù)大于核心線程數(shù)(正式員工),他就會判斷任務(wù)隊列是否已滿,如果未滿就將任務(wù)存放隊列即可。

3. 判斷線程池的實際線程數(shù)是否大于最大線程數(shù)(正式員工 + 臨時員工),如果小于最大線程數(shù),直接創(chuàng)建線程執(zhí)行任務(wù);實際線程數(shù)已經(jīng)等于最大線程數(shù),則會直接執(zhí)行拒絕策略。

d. 拒絕策略

(4種 JDK 提供的拒絕策略 + 1 種 自定義拒絕策略)

Java ?帶的拒絕策略,CallerRunsPolicy:

public class ThreadPoolDemo14 {
    public static void main(String[] args) {
        ThreadFactory factory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                return thread;
            }
        };
        // 手動創(chuàng)建線程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 100, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1), new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < 4; i++) {
            int finalI = i;
            executor.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "執(zhí)行任務(wù):" + finalI);
            });
        }
        //終止線程
        executor.shutdown();
    }
}
輸出
main執(zhí)行任務(wù):2
pool-1-thread-1執(zhí)行任務(wù):0
main執(zhí)行任務(wù):3
pool-1-thread-1執(zhí)行任務(wù):1

?定義拒絕策略:

線程狀態(tài)

shutdown 執(zhí)?時線程池終?接收新任務(wù),并且會將任務(wù)隊列中的任務(wù)處理完; shutdownNow 執(zhí)?時線程池終?接收新任務(wù),并且會給終?執(zhí)?任務(wù)隊列中的任務(wù)。

1. RUNNING:這個沒什么好說的,這是最正常的狀態(tài):接受新的任務(wù),處理等待隊列中的任務(wù); SHUTDOWN:不接受新的任務(wù)提交,但是會繼續(xù)處理等待隊列中的任務(wù); 2. STOP:不接受新的任務(wù)提交,不再處理等待隊列中的任務(wù),中斷正在執(zhí)?任務(wù)的線程;

3. TIDYING:所有的任務(wù)都銷毀了,workCount 為 0。線程池的狀態(tài)在轉(zhuǎn)換為 TIDYING 狀態(tài)時,會執(zhí) ?鉤??法 terminated();

4. TERMINATED:terminated() ?法結(jié)束后,線程池的狀態(tài)就會變成這個。

到此這篇關(guān)于詳解Java線程池的使用(7種創(chuàng)建方法)的文章就介紹到這了,更多相關(guān)Java線程池的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringMVC圖片文件跨服務(wù)器上傳

    SpringMVC圖片文件跨服務(wù)器上傳

    這篇文章主要為大家詳細介紹了SpringMVC圖片文件跨服務(wù)器上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Spring5新特性之Reactive響應式編程

    Spring5新特性之Reactive響應式編程

    這篇文章主要介紹了Spring5新特性之Reactive響應式編程,響應式編程是一種編程范式,通用和專注于數(shù)據(jù)流和變化的,并且是異步的,下文更多詳細內(nèi)容,需要的小伙伴可以參考一下,希望對你有所幫助
    2022-03-03
  • 關(guān)于jpa?querydsl嵌套查詢demo

    關(guān)于jpa?querydsl嵌套查詢demo

    這篇文章主要介紹了關(guān)于jpa?querydsl?嵌套查詢demo,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 關(guān)于spring?aop兩種代理混用的問題

    關(guān)于spring?aop兩種代理混用的問題

    這篇文章主要介紹了關(guān)于spring?aop兩種代理混用的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java利用遺傳算法求解最短路徑問題

    Java利用遺傳算法求解最短路徑問題

    遺傳算法(Genetic Algorithm,GA)最早是由美國的John holland于20世紀70年代提出,該算法是根據(jù)大自然中生物體進化規(guī)律而設(shè)計提出的。本文將利用遺傳算法求解最短路徑問題,需要的可以參考一下
    2022-06-06
  • Spring Boot集成spring-boot-devtools開發(fā)時實現(xiàn)熱部署的方式

    Spring Boot集成spring-boot-devtools開發(fā)時實現(xiàn)熱部署的方式

    這篇文章主要介紹了Spring Boot集成spring-boot-devtools開發(fā)時實現(xiàn)熱部署的方式,文中還給大家提到了spring boot 實現(xiàn)熱部署的方式及集成注意事項,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-05-05
  • Fluent Mybatis 批量更新的使用

    Fluent Mybatis 批量更新的使用

    本文主要介紹了Fluent Mybatis 批量更新的使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • java中注解機制及其原理的詳解

    java中注解機制及其原理的詳解

    這篇文章主要介紹了java中注解機制及其原理的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • 詳解spring cloud構(gòu)建微服務(wù)架構(gòu)的網(wǎng)關(guān)(API GateWay)

    詳解spring cloud構(gòu)建微服務(wù)架構(gòu)的網(wǎng)關(guān)(API GateWay)

    這篇文章主要介紹了詳解spring cloud構(gòu)建微服務(wù)架構(gòu)的網(wǎng)關(guān)(API GateWay),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • java實現(xiàn)簡單的學生管理系統(tǒng)

    java實現(xiàn)簡單的學生管理系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)簡單的學生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評論