亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java詳解使用線程池處理任務(wù)方法

 更新時(shí)間:2022年05月07日 11:38:33   作者:遇安.112  
java中經(jīng)常需要用到多線程來處理,我們非常不建議單純使用繼承Thread或者實(shí)現(xiàn)Runnable接口的方式來創(chuàng)建線程,那樣勢(shì)必有創(chuàng)建及銷毀線程耗費(fèi)資源、線程上下文切換問題。同時(shí)創(chuàng)建過多的線程也可能引發(fā)資源耗盡的風(fēng)險(xiǎn),這個(gè)時(shí)候引入線程池比較合理,方便線程任務(wù)的管理

什么是線程池?

線程池就是一個(gè)可以復(fù)用線程的技術(shù)。

不使用線程池的問題:

如果用戶每發(fā)起一個(gè)請(qǐng)求,后臺(tái)就創(chuàng)建一個(gè)新線程來處理,下次新任務(wù)來了又要?jiǎng)?chuàng)建新線程,而創(chuàng)建新線程的開銷是很大的,這樣會(huì)嚴(yán)重影響系統(tǒng)的性能。

線程池常見面試題:

1、臨時(shí)線程什么時(shí)候創(chuàng)建?

新任務(wù)提交時(shí)發(fā)現(xiàn)核心線程都在忙,任務(wù)隊(duì)列也滿了,并且還可以創(chuàng)建臨時(shí)線程,此時(shí)才會(huì)創(chuàng)建臨時(shí)線程。

2、什么時(shí)候會(huì)開始拒絕任務(wù)?

核心線程和臨時(shí)線程都在忙,任務(wù)隊(duì)列也滿了,新的任務(wù)過來的時(shí)候才會(huì)開始任務(wù)拒絕。

1、線程池處理Runnable任務(wù)

import java.util.concurrent.*;
public class 多線程_5線程池處理Runnable任務(wù) {
    public static void main(String[] args) {
        //線程池處理Runnable任務(wù)
        //創(chuàng)建線程池對(duì)象
        /*
         public ThreadPoolExecutor(int corePoolSize,//核心線程數(shù)量
                              int maximumPoolSize,//線程池可支持的最大線程數(shù)量
                              long keepAliveTime,//臨時(shí)線程的最大存活時(shí)間
                              TimeUnit unit,//指定存活時(shí)間的單位(秒,分等)
                              BlockingQueue<Runnable> workQueue,//指定任務(wù)隊(duì)列
                              ThreadFactory threadFactory,//指定用哪個(gè)線程工廠創(chuàng)建線程
                              RejectedExecutionHandler handler)//指定線程忙,任務(wù)滿了的時(shí)候,新任務(wù)來了怎么辦
         */
        ExecutorService pool=new ThreadPoolExecutor(3,5,
                6, TimeUnit.SECONDS,new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
        //給任務(wù)線程池處理
        Runnable r=new MyExe();
        //三個(gè)核心線程
        pool.execute(r);
        pool.execute(r);
        pool.execute(r);
        //五個(gè)任務(wù)隊(duì)列(不創(chuàng)建臨時(shí)線程時(shí),會(huì)發(fā)現(xiàn)只有三個(gè)線程,即核心線程量)
        pool.execute(r);
        pool.execute(r);
        pool.execute(r);
        pool.execute(r);
        pool.execute(r);
        //創(chuàng)建臨時(shí)線程(五個(gè)線程,即最大線程量)
        pool.execute(r);
        pool.execute(r);
        //不創(chuàng)建,拒絕策略被觸發(fā)
       // pool.execute(r);
        //關(guān)閉線程池(開發(fā)中一般不會(huì)使用)
//        pool.shutdownNow();//立即關(guān)閉,即使任務(wù)沒有執(zhí)行完畢。會(huì)丟失任務(wù)的!
//        pool.shutdown();//會(huì)等待任務(wù)全部執(zhí)行完畢后再關(guān)閉(建議使用)
    }
}
class MyExe implements Runnable{
    public void run(){
        for (int i = 1; i <=6 ; i++) {
            System.out.println(Thread.currentThread().getName()+"正在執(zhí)行:"+i+"次");
        }
        //因?yàn)楫?dāng)前案例任務(wù)太簡單,我們需要?jiǎng)?chuàng)建臨時(shí)隊(duì)列需要讓三個(gè)核心線程忙,五個(gè)任務(wù)隊(duì)列排滿,所以讓線程休眠以增加任務(wù)時(shí)間
        try {
            System.out.println(Thread.currentThread().getName()+"任務(wù)與線程綁定,線程進(jìn)入了休眠");
            Thread.sleep(1000000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2、線程池處理Callable任務(wù)

import java.util.concurrent.*;
public class 多線程_5線程池處理Callable任務(wù) {
    public static void main(String[] args) throws Exception {
        //線程池處理Callable任務(wù)
        //創(chuàng)建線程池對(duì)象
        /*
         public ThreadPoolExecutor(int corePoolSize,//核心線程數(shù)量
                              int maximumPoolSize,//線程池可支持的最大線程數(shù)量
                              long keepAliveTime,//臨時(shí)線程的最大存活時(shí)間
                              TimeUnit unit,//指定存活時(shí)間的單位(秒,分等)
                              BlockingQueue<Runnable> workQueue,//指定任務(wù)隊(duì)列
                              ThreadFactory threadFactory,//指定用哪個(gè)線程工廠創(chuàng)建線程
                              RejectedExecutionHandler handler)//指定線程忙,任務(wù)滿了的時(shí)候,新任務(wù)來了怎么辦
         */
        ExecutorService pool=new ThreadPoolExecutor(3,5,
                6, TimeUnit.SECONDS,new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
        //給任務(wù)線程池處理
//        Callable c=new MyCallable2(100);
//        pool.submit(c);
       Future<String> f1=pool.submit(new MyCallable2(100));
        Future<String> f2=pool.submit(new MyCallable2(200));
        Future<String> f3=pool.submit(new MyCallable2(300));
        Future<String> f4=pool.submit(new MyCallable2(400));
        Future<String> f5=pool.submit(new MyCallable2(500));
//        String str=f1.get();
//        System.out.println(str);
        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
        System.out.println(f5.get());
    }
}
class MyCallable2 implements Callable<String> {
    //                               v(泛型)
    private int n;
    public MyCallable2(int n) {
        this.n = n;
    }
    //重寫call方法
    //案例:加法
    public String call() throws Exception {
        int sum = 0;
        for (int i = 1; i <=n; i++) {
            sum += i;
        }
        return Thread.currentThread().getName()+"執(zhí)行 1-"+n+"的和,結(jié)果為:" + sum;
    }
}

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

相關(guān)文章

最新評(píng)論