JavaEE 初阶篇-深入了解线程池(线程池创建、线程池如何处理任务)

news2024/10/5 13:33:00

🔥博客主页: 【小扳_-CSDN博客】
❤感谢大家点赞👍收藏⭐评论✍

文章目录

        1.0 线程池概述

        1.1 线程池的优点

        1.2 不使用线程池的问题

        1.3 线程池的工作原理图

        1.4 如何创建线程池? 

        2.0 通过 ThreadPoolExecutor 类自定义创建线程池

        2.1 ThreadPoolExecutor 构造器

        3.0 线程池处理 Runnable 任务

        3.1 通过 void execute(Runnable command) 方法

        3.2 通过 void shutdown() 方法

        3.3 通过 List shutdownNow() 方法

        3.4 临时线程什么时候创建?

        3.5 什么时候会开始拒绝新任务?

        4.0 线程池处理 Callable 任务

        4.1 通过 submit() 方法

        5.0 通过 Executors 工具类创建出线程池

        5.1 通过 Executors.newFixedThreadPool(int nThreads) 方法来创建线程池

        5.2 通过 Executors.newCachedThreadPool() 方法来创建线程池

        5.3 通过 Eexcutors.newSingleThreadExecutor() 方法来创建线程池

        6.0 新任务拒绝策略

        6.1 AbortPolicy(默认策略,直接抛出异常)

        6.2 DiscardPolicy(直接丢弃任务)

        6.3 CallerRunsPolicy(由调用线程执行任务)

        6.4 DiscardOldestPolicy(丢弃队列中最旧的任务)


        1.0 线程池概述

        线程池是一种重要的并发编程机制,用于管理和复用线程,以提高应用程序的性能和资源利用率。

        线程池就是一个可以复用线程的技术。

        1.1 线程池的优点

        1)降低线程创建和销毁的开销。通过重用线程从而减少频繁创建和开销线程所带来的性能损耗。

        2)控制并发线程数量。通过设定线程池的大小和配置,可以限制并发线程的数量,避免系统资源耗尽。

        1.2 不使用线程池的问题

        假设用户发起一个请求,后台就需要创建一个新线程来处理,下次新任务来了肯定又要创建新线程处理的,而创建新线程的开销是很大的,并且请求过多时,肯定会产生大量的线程出来,这样会严重影响系统的性能。

        1.3 线程池的工作原理图

        1.4 如何创建线程池? 

        常见的创建线程池的方式有两种:

        1)通过已经实现 ExecutorService 接口的 ThreadPoolExecutor 类来创建出线程池。

        2)通过 Executors 工具类创建出线程池。

 

        2.0 通过 ThreadPoolExecutor 类自定义创建线程池

        可以直接使用 ThreadPoolExecutor 类来创建自定义的线程池,通过指定核心线程数、最大线程数、工作队列等参数来满足特定的需求。这种方法更灵活,可以根据具体场景进行定制化配置。

        2.1 ThreadPoolExecutor 构造器

        1)参数一:corePoolSize:指定线程池的核心线程的数量。

        2)参数二:maximumPoolSize:指定线程池的最大线程数量。

        3)参数三:keepAliveTime:指定临时线程的存活时间。

        4)参数四:unit:指定临时线程的存活时间单位(秒、分、时、天)。

        5)参数五:workQueue:指定线程池的任务队列。

        6)参数六:threadFactory:指定线程池的线程工厂(创建线程的地方)。

        7)参数七:handler:指定线程池的任务拒绝策略(线程都在忙,任务队列也满的时候,新任务来了该怎么处理)。

具体创建线程池代码如下:

import java.util.concurrent.*;
public class CreateThreadPoolExecutor {

    public Executor threadPool = new ThreadPoolExecutor(
            3,//核心线程数量
            5,//最大线程数量
            8,//临时线程存活时间
            TimeUnit.SECONDS,//临时线程存活时间单位
            new ArrayBlockingQueue<>(4),//创建任务队列,最大存放任务数量为4
            Executors.defaultThreadFactory(),//线程工厂,
            //用到了Executors工具类来创建默认线程工厂
            new ThreadPoolExecutor.AbortPolicy());//拒绝策略,默认策略:抛出异常
    
}

        

        3.0 线程池处理 Runnable 任务

        线程池通过执行 Runnable 任务来实现多线程处理。将实现 Runnable 接口的任务提交给线程池,线程池会根据配置的参数调度任务执行。核心线程数内的任务会立即执行,超出核心线程数的任务会被放入任务队列中。如果任务队列已满,且线程数未达到最大线程数,线程池会创建新线程执行任务。线程池管理线程的生命周期,重用线程以减少线程创建和销毁的开销。

ExecutorService 的常用方法:

        3.1 通过 void execute(Runnable command) 方法

        将 Runnable 类型的任务交给线程池,线程池就会自动创建线程,自动执行 run() 方法且自动启动线程。

代码如下:

import java.util.concurrent.*;
public class CreateThreadPoolExecutor {
    public static void main(String[] args) {
        Executor threadPool = new ThreadPoolExecutor(3,//核心线程数量
                5,//最大线程数量
                8,//临时线程存活时间
                TimeUnit.SECONDS,//临时线程存活时间单位
                new ArrayBlockingQueue<>(4),//创建任务队列,最大存放任务数量为4
                Executors.defaultThreadFactory(),//线程工厂,
                //用到了Executors工具类来创建默认线程工厂
                new ThreadPoolExecutor.AbortPolicy());//拒绝策略,默认策略:抛出异常

        //定义出Runnable类型的任务
        MyRunnable myRunnable1 = new MyRunnable();
        MyRunnable myRunnable2 = new MyRunnable();
        //将该任务提交给线程池
        threadPool.execute(myRunnable1);
        threadPool.execute(myRunnable2);
        
    }
}
class  MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "正在执行");
    }
}

运行结果:

        需要注意的是,任务执行完毕之后,线程池中的线程不会销毁,还在继续运行中

        3.2 通过 void shutdown() 方法

        等待全部任务执行完毕后,再关闭线程池。想要手动关闭线程池中的线程,可以用该方法,等待全部任务都执行后才会关闭。

代码如下:

import java.util.concurrent.*;
public class CreateThreadPoolExecutor {
    public static void main(String[] args) {
        Executor threadPool = new ThreadPoolExecutor(3,//核心线程数量
                5,//最大线程数量
                8,//临时线程存活时间
                TimeUnit.SECONDS,//临时线程存活时间单位
                new ArrayBlockingQueue<>(4),//创建任务队列,最大存放任务数量为4
                Executors.defaultThreadFactory(),//线程工厂,
                //用到了Executors工具类来创建默认线程工厂
                new ThreadPoolExecutor.AbortPolicy());//拒绝策略,默认策略抛出异常

        //定义出Runnable类型的任务
        MyRunnable myRunnable1 = new MyRunnable();
        MyRunnable myRunnable2 = new MyRunnable();
        //将该任务提交给线程池
        threadPool.execute(myRunnable1);
        threadPool.execute(myRunnable2);

        //当全部任务都执行结束后,才会关闭
        ((ThreadPoolExecutor) threadPool).shutdown();

    }
}
class  MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "正在执行");
    }
}

运行结果:

        3.3 通过 List<Runnable> shutdownNow() 方法

        立刻关闭线程池,停止正在执行的任务,并返回队列中未执行的任务。先比较与以上的方法,当前的方法就很“激进”了。不管任务结束与否,都会关闭线程池中的线程。

代码如下:

import java.util.List;
import java.util.concurrent.*;
public class CreateThreadPoolExecutor {
    public static void main(String[] args) {
        Executor threadPool = new ThreadPoolExecutor(
            3,//核心线程数量
            5,//最大线程数量
            8,//临时线程存活时间
            TimeUnit.SECONDS,//临时线程存活时间单位
            new ArrayBlockingQueue<>(4),//创建任务队列,最大存放任务数量为4
            Executors.defaultThreadFactory(),//线程工厂,
            //用到了Executors工具类来创建默认线程工厂
            new ThreadPoolExecutor.AbortPolicy());//拒绝策略,默认策略:抛出异常

        //定义出Runnable类型的任务
        MyRunnable myRunnable1 = new MyRunnable();
        MyRunnable myRunnable2 = new MyRunnable();
        MyRunnable myRunnable3 = new MyRunnable();
        MyRunnable myRunnable4 = new MyRunnable();
        MyRunnable myRunnable5 = new MyRunnable();
        //将该任务提交给线程池
        threadPool.execute(myRunnable1);
        threadPool.execute(myRunnable2);
        threadPool.execute(myRunnable3);
        threadPool.execute(myRunnable4);
        threadPool.execute(myRunnable5);
        //还没执行完的任务会交给l链表
        List<Runnable> l = ((ThreadPoolExecutor) threadPool).shutdownNow();
        for (int i = 0; i < l.size(); i++) {
            System.out.println("还没来得及执行的任务:" + l.get(i));
        }
    }
}
class  MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "正在执行");
    }
}

运行结果:

        3.4 临时线程什么时候创建?

        新任务提交时发现核心线程都在忙,任务队列也满了,并且还可以创建临时线程,此时才会创建临时线程。

代码如下:

import java.util.concurrent.*;
public class T {
    public static void main(String[] args) {
        Executor threadPool = new ThreadPoolExecutor(3,//核心线程数量
                5,//最大线程数量
                8,//临时线程存活时间
                TimeUnit.SECONDS,//临时线程存活时间单位
                new ArrayBlockingQueue<>(4),//创建任务队列,最大存放任务数量为4
                Executors.defaultThreadFactory(),//线程工厂,
                //用到了Executors工具类来创建默认线程工厂
                new ThreadPoolExecutor.AbortPolicy());//拒绝策略,默认策略抛出异常

        //定义出Runnable类型的任务,此时该三个任务都被核心线程正在处理
        MyRunnable myRunnable1 = new MyRunnable();
        MyRunnable myRunnable2 = new MyRunnable();
        MyRunnable myRunnable3 = new MyRunnable();
        //这里还没到临时线程创建的时机,被放入到任务队列中等待被核心线程执行
        MyRunnable myRunnable4 = new MyRunnable();
        MyRunnable myRunnable5 = new MyRunnable();
        MyRunnable myRunnable6 = new MyRunnable();
        MyRunnable myRunnable7 = new MyRunnable();

        //此时到了临时线程创建的时机了,核心线程都在满,队列已经满了
        //创建出第一个临时线程
        MyRunnable myRunnable8 = new MyRunnable();
        //创建出第二个临时线程
        MyRunnable myRunnable9 = new MyRunnable();
        
        //将该任务提交给线程池
        threadPool.execute(myRunnable1);
        threadPool.execute(myRunnable2);
        threadPool.execute(myRunnable3);
        threadPool.execute(myRunnable4);
        threadPool.execute(myRunnable5);
        threadPool.execute(myRunnable6);
        threadPool.execute(myRunnable7);
        threadPool.execute(myRunnable8);
        threadPool.execute(myRunnable9);

    }
}

class  MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "==> 
                                                正在执行" + this);
        try {
            //在这里等待10s
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

运行结果:

        当前的核心线程为 3 ,最大线程为 5 且任务队列有四个位置。当核心线程达到 3 时,且任务队列满 4 个了,还能创建临时线程时提交新任务就可以创建出临时线程了。

        3.5 什么时候会开始拒绝新任务?

        核心线程和临时线程都在忙,任务队列也满了,新的任务过来的时候才会开始拒绝任务。

代码如下:

import java.util.concurrent.*;
public class T {
    public static void main(String[] args) {
        Executor threadPool = new ThreadPoolExecutor(3,//核心线程数量
                5,//最大线程数量
                8,//临时线程存活时间
                TimeUnit.SECONDS,//临时线程存活时间单位
                new ArrayBlockingQueue<>(4),//创建任务队列,最大存放任务数量为4
                Executors.defaultThreadFactory(),//线程工厂,
                //用到了Executors工具类来创建默认线程工厂
                new ThreadPoolExecutor.AbortPolicy());//拒绝策略,默认策略抛出异常

        //定义出Runnable类型的任务,此时该三个任务都被核心线程正在处理
        MyRunnable myRunnable1 = new MyRunnable();
        MyRunnable myRunnable2 = new MyRunnable();
        MyRunnable myRunnable3 = new MyRunnable();
        //这里还没到临时线程创建的时机,被放入到任务队列中等待被核心线程执行
        MyRunnable myRunnable4 = new MyRunnable();
        MyRunnable myRunnable5 = new MyRunnable();
        MyRunnable myRunnable6 = new MyRunnable();
        MyRunnable myRunnable7 = new MyRunnable();

        //此时到了临时线程创建的时机了,核心线程都在满,队列已经满了
        //创建出第一个临时线程
        MyRunnable myRunnable8 = new MyRunnable();
        //创建出第二个临时线程
        MyRunnable myRunnable9 = new MyRunnable();

        //此时核心线程都在忙,且队列已经占满了,再提交新任务的时候,
        //就会采取拒绝策略
        MyRunnable myRunnable10 = new MyRunnable();

        //将该任务提交给线程池
        threadPool.execute(myRunnable1);
        threadPool.execute(myRunnable2);
        threadPool.execute(myRunnable3);
        threadPool.execute(myRunnable4);
        threadPool.execute(myRunnable5);
        threadPool.execute(myRunnable6);
        threadPool.execute(myRunnable7);
        threadPool.execute(myRunnable8);
        threadPool.execute(myRunnable9);
        threadPool.execute(myRunnable10);

    }
}

class  MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "==> 
                                                    正在执行" + this);
        try {
            //在这里等待10s
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

运行结果:

        拒绝策略默认是抛出异常

        4.0 线程池处理 Callable 任务

        处理Callable任务时,线程池可通过 submit() 方法提交 Callable 实例,并返回代表任务执行情况的 Future 。通过 Future 可以获取任务执行结果或处理异常

ExecutorService 常见的方法:

        4.1 通过 submit() 方法

        提交 Callable 类型的任务给线程池,线程池会选择可用线程、自动执行 call() 方法、自动启动线程。

代码如下:

import java.util.concurrent.*;

public class MyCreateThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService executor = new ThreadPoolExecutor(
                3,
                5,
                8,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        //创建出Callable类型的实例对象
        MyCallable callable1 = new MyCallable(100);
        MyCallable callable2 = new MyCallable(200);
        MyCallable callable3 = new MyCallable(300);
        MyCallable callable4 = new MyCallable(400);

        //将任务提交到线程池中
        Future<String> f1 = executor.submit(callable1);
        Future<String> f2 = executor.submit(callable2);
        Future<String> f3 = executor.submit(callable3);
        Future<String> f4 = executor.submit(callable4);

        //拿到结果
        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());

    }
}

运行结果:

        5.0 通过 Executors 工具类创建出线程池

        Executors 工具类提供了创建不同类型线程池的方法,如 newFixedThreadPool 、 newCachedThreadPool、newSingleThreadExecutor 等。这些方法返回不同配置的线程池实例,可用于执行 Runnable 和 Callable 任务。通过 Executors 创建线程池,可方便地管理线程池的大小、任务队列、线程工厂等参数。注意合理选择线程池类型以满足任务需求,避免资源浪费或任务阻塞。

        简单来说,Executors 工具类为我们提供了不同特点的线程池。

        5.1 通过 Executors.newFixedThreadPool(int nThreads) 方法来创建线程池

        创建固定线程数量的线程池,如果某个线程因执行异常而结束,那么线程池会补充一个新线程代替它。

代码如下:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class demo1 {
    public static void main(String[] args) {

        //固定的线程池核心线程为3,临时线程为0,所以最大线程数量也就是3。
        ExecutorService pool = Executors.newFixedThreadPool(3);

        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + 
                                                        "==> 正在执行");
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + 
                                                        "==> 正在执行");
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + 
                                                        "==> 正在执行");
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + 
                                                        "==> 正在执行");
            }
        });
        
    }
}

运行结果:

        其实 Executors.newFixedThreadPool(int nThreads) 方法的底层就是通过 ThreadPoolExecutor 类来实现创建线程池的。传入的参数就是核心线程线程数量,也为最大线程数量,因此临时线程数量为 0 。因此没有临时线程的存在,那么该线程池中的线程很固定

如图:

        5.2 通过 Executors.newCachedThreadPool() 方法来创建线程池

        线程数量随着任务增加而增加,如果线程任务执行完毕且空闲了 60 s则会被回收掉。也就是有多少了任务线程池会根据任务数量动态创建新线程。

代码如下:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class demo2 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));

        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));

        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));

        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));

        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));

        System.out.println( ((ThreadPoolExecutor)executorService).getPoolSize());
    }
}

        通过 getPoolSize() 方法可以拿到当前线程池中线程的数量。

运行结果如下:

        该方法创建出来的线程池特点为,当任务越多时候,线程池中的线程数量也会随之增加。其实这个方法的底层实现也是通过 ThreadPoolExecutor 类来实现创建线程池。

如图:

        很惊奇的发现,核心线程竟然是 0 个,而临时线程数量的最大值是非常非常大的,如果线程任务执行完毕且空闲了 60 s则会被回收掉。

        5.3 通过 Eexcutors.newSingleThreadExecutor() 方法来创建线程池

        创建只有一个线程的线程池对象,如果该线程出现异常而结束,那么线程池会补充一个新线程。

代码如下:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class demo3 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));
        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));
        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));
        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));
        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));
        executorService.execute(() -> System.out.println(Thread.currentThread().getName() + " ==> 正在执行"));
        
    }
}

运行结果:

        因为该线程池中只有一个线程,所以该线程需要执行所有的任务。因此该方法创建出来的线程池特点是,只有一个线程。当然该方法的底层也是通过 ThreadPoolExecutor 类来试下创建线程池。

如图:

        核心线程只有一个,最大线程也是一个,说明临时线程为零个。因此线程池中只有单一的线程。

        6.0 新任务拒绝策略

        常见的拒绝策略包括:AbortPolicy(默认策略,直接抛出异常)、CallerRunsPolicy(由调用线程执行任务)、DiscardPolicy(直接丢弃任务)、DiscardOldestPolicy(丢弃队列中最旧的任务)。

        6.1 AbortPolicy(默认策略,直接抛出异常)

        丢弃任务并抛出 RejectedExecutionExeception 异常,是默认的策略。

代码如下:

import java.util.concurrent.*;

public class demo1 {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                2,
                3,
                8,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                //默认拒绝新任务的策略
                new ThreadPoolExecutor.AbortPolicy()
        );
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //此时会有临时线程创建
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //此时新任务提交的时候,会触发拒绝策略
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }
}

运行结果:

        新提交的任务就被抛弃了且抛出异常。

        6.2 DiscardPolicy(直接丢弃任务)

        丢弃任务,但是不抛出异常,这是不推荐的做法。

代码如下:

import java.util.concurrent.*;

public class demo1 {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                2,
                3,
                8,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                
                new ThreadPoolExecutor.DiscardPolicy()
        );
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //此时会有临时线程创建
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //此时新任务提交的时候,会触发拒绝策略
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }
}

运行结果:

        6.3 CallerRunsPolicy(由调用线程执行任务)

        由主线程负责调用任务的 run() 方法从而绕过线程池直接执行。

        简单来说,就是交给 main 主线程执行该任务。

代码如下:

import java.util.concurrent.*;

public class demo1 {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                2,
                3,
                8,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //此时会有临时线程创建
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //此时新任务提交的时候,会触发拒绝策略
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }
}

        6.4 DiscardOldestPolicy(丢弃队列中最旧的任务)

        抛弃队列中等待最久的任务,然后把当前任务假如队列中。

import java.util.concurrent.*;

public class demo1 {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                2,
                3,
                8,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                //默认拒绝新任务的策略
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //此时会有临时线程创建
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //此时新任务提交的时候,会触发拒绝策略
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }
}

运行结果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1574530.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据结构与算法笔记:递归函数设计技巧

ACM金牌带你零基础直达C语言精通-课程资料 本笔记属于船说系列课程之一&#xff0c;课程链接&#xff1a; 哔哩哔哩_bilibilihttps://www.bilibili.com/cheese/play/ep66799?csourceprivate_space_class_null&spm_id_from333.999.0.0 你也可以选择购买『船说系列课程-年度…

Tensorflow2.0笔记 - 自定义Layer和Model实现CIFAR10数据集的训练

本笔记记录使用自定义Layer和Model来做CIFAR10数据集的训练。 CIFAR10数据集下载&#xff1a; https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 自定义的Layer和Model实现较为简单&#xff0c;参数量较少&#xff0c;并且没有卷积层和dropout等&#xff0c;最终准确率…

穿越代码之海:探寻结构体深层逻辑,展望未来应用新天地

欢迎来到白刘的领域 Miracle_86.-CSDN博客 系列专栏 C语言知识 先赞后看&#xff0c;已成习惯 创作不易&#xff0c;多多支持&#xff01; 结构体作为一种数据结构&#xff0c;其定义和特点决定了它在各种应用中的广泛适用性。随着科技的进步和新兴行业的不断涌现&#xf…

测试自动化流程设计思路

a) 背景介绍 基于当前互联网敏捷开发的现状&#xff0c;手工人力测试已不足以满足当前快速的版本迭代&#xff1b;以下将介绍一种可实现的自动化设计与使用。 b) 当前版本迭代流程 研发同学从代码库master分支拉出新代码进行研发工作得开发开发完成之后提交到代码库测试同学介入…

从概念到实践:探索独立站在当代电商中的关键作用

随着数字化时代的到来&#xff0c;电子商务已成为全球商业生态的核心组成部分。在这个不断变化的市场中&#xff0c;独立站作为企业建立在线身份和拓展业务的强大工具&#xff0c;正逐步展现出其不可替代的价值。 从概念到实践&#xff0c;本文将深入探索独立站在当代电商中的关…

C++从入门到精通——类的作用域及类的实例化

类的作用域及类的实例化 前言一、类的作用域二、类的实例化引例类是对对象进行描述的示例 一个类可以实例化出多个对象示例 示例 前言 类的作用域是指类中定义的变量和方法的可见性和可访问性范围。在类的内部&#xff0c;所有成员&#xff08;包括属性和方法&#xff09;都具…

LeetCode-51. N 皇后【数组 回溯】

LeetCode-51. N 皇后【数组 回溯】 题目描述&#xff1a;解题思路一&#xff1a;回溯&#xff0c; 回溯三部曲。验证是否合法只需要检查:1.正上方&#xff1b;2. 左上方&#xff1b;3.右上方。因为是从上到下&#xff0c;从左到右遍历的&#xff0c;下方不可能有皇后。解题思路…

Day60:WEB攻防-XMLXXE安全无回显方案OOB盲注DTD外部实体黑白盒挖掘

目录 XML&XXE-传输-原理&探针&利用&玩法 XXE 黑盒发现 XXE 白盒发现 XXE修复防御方案 有回显 无回显 XML&XXE-黑盒-JSON&黑盒测试&类型修改 XML&XXE-白盒-CMS&PHPSHE&无回显 知识点&#xff1a; 1、XXE&XML-原理-用途&…

Unity与CocosCreator对比学习二

一、锚点与适配 1.在Creator中 适配通过锚点、位置和Widget达到适配目的&#xff1b;锚点是节点在其父节点坐标系中坐标对其点&#xff0c;其x,y范围在[0, 1]之间&#xff1b; 锚点为(0, 0)时在节点自身的左下角&#xff0c;节点坐标指其左下角在父节点中的坐标&#xff1b;锚…

【2024系统架构设计】案例分析- 5 Web应用

目录 一 基础知识 二 真题 一 基础知识 1 Web应用技术分类 大型网站系统架构的演化:高性能、高可用、可维护、应变、安全。 从架构来看:MVC,MVP,MVVM,REST,Webservice,微服务。

内存管理(SRAM)

内存管理介绍 内存管理实际上就是指管理SRAM. 内存管理&#xff0c;是指软件运行时对计算机内存资源的分配和使用的技术。其最主要的目的是如 何高效、快速的分配&#xff0c;并且在适当的时候释放和回收内存资源。内存管理的实现方法有很多种&#xff0c;其实最终都是要实现两…

用vscode仿制小米官网

html内容: <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><link rel&quo…

无线基本认识和配置

1、无线局域网 IEEE 802.11标准 根据应用范围分类 WPAN --- 个人无线网络 NFC、ZIgbee、Bluetooth WLAN --- 无线局域网 WiFi&#xff0c;使用到WPAN技术 WMAN --- 无线城域网 WiMax 802.16 WWAN --- 无线广域网 GSM、CDMA、WCDMA、LTE、5G、TD-SCDMA 2、…

PG 中的 MAXALIGN 及对齐分配内存(MemoryContextAllocAligned)

在PG源码中&#xff0c;MAXALIGN这个宏&#xff0c;返回最接近输入数字&#xff08;大于&#xff09;且能整除8的数&#xff0c;仅此而已。 常用于内存相关的计算&#xff0c;在PG代码中使用的相当广泛&#xff0c;为啥要用这个MAXALIGN&#xff1f;我估计是基于 “CPU访问对齐…

数据库同步方案Sqlserver

数据库同步方案探究 随着信息技术的迅猛发展&#xff0c;数据库在各个领域的应用日益广泛。而在分布式系统、云计算、大数据等场景下&#xff0c;数据库同步成为了一个至关重要的问题。数据库同步不仅关乎数据的完整性和一致性&#xff0c;还直接影响到系统的稳定性和性能。因…

加薪非要老总批?--责任链模式

1.1 老板&#xff0c;我要加薪 "我和刚进来的几个同事比较&#xff0c;我觉得我做得很好。公司每每分配的任务&#xff0c;我基本都可以快速完成。有一次&#xff0c;一段程序需要增加一个分支条件&#xff0c;我立刻想到利用反射、工厂等设计模式来处理&#xff0c;经理对…

移除元素 -- 力扣第27题 -- 暴力、双指针解法

题目 https://leetcode.cn/problems/remove-element/description/ 给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素&#xff0c;并返回移除后数组的新长度。 不要使用额外的数组空间&#xff0c;你必须仅使用 O(1) 额外空间并原地修改输…

Maven--lib分离的打包方式

就是把lib包和source源码分开打包。优势就是&#xff0c;面对频繁更新的应用场景时&#xff0c;可以只更新源码包&#xff08;当然&#xff0c;前提是你的依赖没有增减&#xff09;。尤其是使用jenkins更新项目时&#xff0c;会省去很多时间吧&#xff1f; 不同项目的 lib之间不…

yolov9直接调用zed相机实现三维测距(python)

yolov9直接调用zed相机实现三维测距&#xff08;python&#xff09; 1. 相关配置2. 相关代码2.1 相机设置2.2 测距模块2.2 实验结果 相关链接 此项目直接调用zed相机实现三维测距&#xff0c;无需标定&#xff0c;相关内容如下&#xff1a; 1. yolov4直接调用zed相机实现三维测…

传统海外仓的管理模式有什么缺点?使用位像素海外仓系统的海外仓有什么优势?

传统的海外仓管理模式主要需要大量的人工操作和相对简单的信息化手段进行仓库的日常运营。因此&#xff0c;传统海外仓的运作比较依赖仓库员工的手工记录、核对和处理各种仓储和物流信息。 然而&#xff0c;传统海外仓管理模式通常存在一些缺点&#xff1a; 效率低下 因为需…