「java线程池自定义」自定义线程池怎么写
本篇文章给大家谈谈java线程池自定义,以及自定义线程池怎么写对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
Java线程:新特征-线程池
; Sun在Java 中 对Java线程的类库做了大量的扩展 其中线程池就是Java 的新特征之一 除了线程池之外 还有很多多线程相关的内容 为多线程的编程带来了极大便利 为了编写高效稳定可靠的多线程程序 线程部分的新增内容显得尤为重要 有关Java 线程新特征的内容全部在ncurrent下面 里面包含数目众多的接口和类 熟悉这部分API特征是一项艰难的学习过程 目前有关这方面的资料和书籍都少之又少 大所属介绍线程方面书籍还停留在java 之前的知识层面上 当然新特征对做多线程程序没有必须的关系 在java 之前通用可以写出很优秀的多线程程序 只是代价不一样而已 线程池的基本思想还是一种对象池的思想 开辟一块内存空间 里面存放了众多(未死亡)的线程 池中线程执行调度由池管理器来处理 当有线程任务时 从池中取一个 执行完成后线程对象归池 这样可以避免反复创建线程对象所带来的性能开销 节省了系统的资源 在Java 之前 要实现一个线程池是相当有难度的 现在Java 为我们做好了一切 我们只需要按照提供的API来使用 即可享受线程池带来的极大便利 Java 的线程池分好多种 固定尺寸的线程池 可变尺寸连接池 在使用线程池之前 必须知道如何去创建一个线程池 在Java 中 需要了解的是ncurrent Executors类的API 这个类提供大量创建连接池的静态方法 是必须掌握的 一 固定大小的线程池 import ncurrent Executors; import ncurrent ExecutorService; /** * Java线程 线程池 * * @author Administrator : : */ public class Test { public static void main(String[] args) { //创建一个可重用固定线程数的线程池 ExecutorService pool = Executors newFixedThreadPool( ); //创建实现了Runnable接口对象 Thread对象当然也实现了Runnable接口 Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); //将线程放入池中进行执行 pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); //关闭线程池 pool shutdown(); } } class MyThread extends Thread{ @Override public void run() { System out println(Thread currentThread() getName()+ 正在执行 ); } } pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 Process finished with exit code 二 单任务线程池 在上例的基础上改一行创建pool对象的代码为 //创建一个使用单个 worker 线程的 Executor 以无界队列方式来运行该线程 ExecutorService pool = Executors newSingleThreadExecutor(); 输出结果为 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 Process finished with exit code 对于以上两种连接池 大小都是固定的 当要加入的池的线程(或者任务)超过池最大尺寸时候 则入此线程池需要排队等待 一旦池中有线程完毕 则排队等待的某个线程会入池执行
三 可变尺寸的线程池 与上面的类似 只是改动下pool的创建方式 //创建一个可根据需要创建新线程的线程池 但是在以前构造的线程可用时将重用它们 ExecutorService pool = Executors newCachedThreadPool(); pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 Process finished with exit code 四 延迟连接池 import ncurrent Executors; import ncurrent ScheduledExecutorService; import ncurrent TimeUnit; /** * Java线程 线程池 * * @author Administrator : : */ public class Test { public static void main(String[] args) { //创建一个线程池 它可安排在给定延迟后运行命令或者定期地执行 ScheduledExecutorService pool = Executors newScheduledThreadPool( ); //创建实现了Runnable接口对象 Thread对象当然也实现了Runnable接口 Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); //将线程放入池中进行执行 pool execute(t ); pool execute(t ); pool execute(t ); //使用延迟执行风格的方法 pool schedule(t TimeUnit MILLISECONDS); pool schedule(t TimeUnit MILLISECONDS); //关闭线程池 pool shutdown(); } } class MyThread extends Thread { @Override public void run() { System out println(Thread currentThread() getName() + 正在执行 ); } } pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 Process finished with exit code
五 单任务延迟连接池 在四代码基础上 做改动 //创建一个单线程执行程序 它可安排在给定延迟后运行命令或者定期地执行 ScheduledExecutorService pool = Executors newSingleThreadScheduledExecutor(); pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 Process finished with exit code 六 自定义线程池 import ncurrent ArrayBlockingQueue; import ncurrent BlockingQueue; import ncurrent ThreadPoolExecutor; import ncurrent TimeUnit; /** * Java线程 线程池 自定义线程池 * * @author Administrator : : */ public class Test { public static void main(String[] args) { //创建等待队列 BlockingQueueRunnable bqueue = new ArrayBlockingQueueRunnable( ); //创建一个单线程执行程序 它可安排在给定延迟后运行命令或者定期地执行 ThreadPoolExecutor pool = new ThreadPoolExecutor( TimeUnit MILLISECONDS bqueue); //创建实现了Runnable接口对象 Thread对象当然也实现了Runnable接口 Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); //将线程放入池中进行执行 pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); //关闭线程池 pool shutdown(); } } class MyThread extends Thread { @Override public void run() { System out println(Thread currentThread() getName() + 正在执行 ); try { Thread sleep( L); } catch (InterruptedException e) { e printStackTrace(); } } }
pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 pool thread 正在执行 Process finished with exit code 创建自定义线程池的构造方法很多 本例中参数的含义如下 ThreadPoolExecutor 用给定的初始参数和默认的线程工厂及处理程序创建新的 ThreadPoolExecutor 使用 Executors 工厂方法之一比使用此通用构造方法方便得多 参数 corePoolSize 池中所保存的线程数 包括空闲线程 maximumPoolSize 池中允许的最大线程数 keepAliveTime 当线程数大于核心时 此为终止前多余的空闲线程等待新任务的最长时间 unit keepAliveTime 参数的时间单位 workQueue 执行前用于保持任务的队列 此队列仅保持由 execute 方法提交的 Runnable 任务 抛出 IllegalArgumentException 如果 corePoolSize 或 keepAliveTime 小于零 或者 maximumPoolSize 小于或等于零 或者 corePoolSize 大于 maximumPoolSize NullPointerException 如果 workQueue 为 null 自定义连接池稍微麻烦些 不过通过创建的ThreadPoolExecutor线程池对象 可以获取到当前线程池的尺寸 正在执行任务的线程数 工作队列等等 有关Java 线程池的内容到此就没有了 更多的内容还需要研读API来获取 lishixinzhi/Article/program/Java/hx/201311/26769
自定义线程池拒绝策略及有界无界队列
核心线程数:实际运行的线程数
最大线程数:最大可以创建的线程数
(1)ThreadPoolExecutor.AbortPolicy 丢弃任务,并抛出 RejectedExecutionException 异常。
(2)ThreadPoolExecutor.CallerRunsPolicy:该任务被线程池拒绝,由调用 execute方法的线程执行该任务。
(3)ThreadPoolExecutor.DiscardOldestPolicy : 抛弃队列最前面的任务,然后重新尝试执行任务。
(4)ThreadPoolExecutor.DiscardPolicy,丢弃任务,不过也不抛出异常。
当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize,如果还有任务到来就会采取任务拒绝策略。
一个任务通过 execute(Runnable) 方法被添加到线程池,任务就是一个 Runnable 类型的对象,任务的执行方法就是 Runnable 类型对象的 run() 方法。
当一个任务通过 execute(Runnable) 方法欲添加到线程池时,线程池采用的策略如下(即添加任务的策略):
如果此时线程池中的数量小于 corePoolSize ,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。
如果此时线程池中的数量等于 corePoolSize ,但是缓冲队列 workQueue 未满,那么任务被放入缓冲队列。
如果此时线程池中的数量大于 corePoolSize ,缓冲队列 workQueue 满,并且线程池中的数量小于maximumPoolSize ,建新的线程来处理被添加的任务。
如果此时线程池中的数量大于 corePoolSize ,缓冲队列 workQueue 满,并且线程池中的数量等于maximumPoolSize ,那么通过 handler 所指定的策略来处理此任务。
任务处理的优先级(顺序)为:
核心线程 corePoolSize 、任务队列 workQueue 、最大线程 maximumPoolSize ,如果三者都满了,使用 handler处理被拒绝的任务。当线程池中的线程数量大于 corePoolSize 时,如果某线程空闲时间超过 keepAliveTime ,线程将被终止。这样,线程池可以动态的调整池中的线程数。
从有界无界上分
常见的有界队列为
ArrayBlockingQueue 基于数组实现的阻塞队列
LinkedBlockingQueue 其实也是有界队列,但是不设置大小时就是无界的。
ArrayBlockingQueue 与 LinkedBlockingQueue 对比一哈
ArrayBlockingQueue 实现简单,表现稳定,添加和删除使用同一个锁,通常性能不如后者
LinkedBlockingQueue 添加和删除两把锁是分开的,所以竞争会小一些
SynchronousQueue 比较奇葩,内部容量为零,适用于元素数量少的场景,尤其特别适合做交换数据用,内部使用 队列来实现公平性的调度,使用栈来实现非公平的调度,在Java6时替换了原来的锁逻辑,使用CAS代替了
上面三个队列他们也是存在共性的
put take 操作都是阻塞的
offer poll 操作不是阻塞的,offer 队列满了会返回false不会阻塞,poll 队列为空时会返回null不会阻塞
补充一点,并不是在所有场景下,非阻塞都是好的,阻塞代表着不占用CPU,在有些场景也是需要阻塞的,put take 存在必有其存在的必然性
常见的无界队列
ConcurrentLinkedQueue 无锁队列,底层使用CAS操作,通常具有较高吞吐量,但是具有读性能的不确定性,弱一致性——不存在如ArrayList等集合类的并发修改异常,通俗的说就是遍历时修改不会抛异常
PriorityBlockingQueue 具有优先级的阻塞队列
DelayedQueue 延时队列,使用场景
缓存:清掉缓存中超时的缓存数据
任务超时处理
补充:内部实现其实是采用带时间的优先队列,可重入锁,优化阻塞通知的线程元素leader
LinkedTransferQueue 简单的说也是进行线程间数据交换的利器,在SynchronousQueue 中就有所体现,并且并发大神 Doug Lea 对其进行了极致的优化,使用15个对象填充,加上本身4字节,总共64字节就可以避免缓存行中的伪共享问题,其实现细节较为复杂,可以说一下大致过程:
比如消费者线程从一个队列中取元素,发现队列为空,他就生成一个空元素放入队列 , 所谓空元素就是数据项字段为空。然后消费者线程在这个字段上旅转等待。这叫保留。直到一个生产者线程意欲向队例中放入一个元素,这里他发现最前面的元素的数据项字段为 NULL,他就直接把自已数据填充到这个元素中,即完成了元素的传送。大体是这个意思,这种方式优美了完成了线程之间的高效协作。参考自
ThreadPoolExcutor用法详解
java线程池用法举例:
1、ThreadPoolExecutor executor =new ThreadPoolExecutor(2,10,30, TimeUnit.SECONDS,new ArrayBlockingQueue(100));
2、ThreadPoolExecutor executor2 =new ThreadPoolExecutor(2,10,30, TimeUnit.SECONDS,new LinkedBlockingDeque());
知道了各个参数的作用后,我们开始构造符合我们期待的线程池。首先看JDK给我们预定义的几种线程池:
一、预定义线程池
FixedThreadPool
publicstaticExecutorServicenewFixedThreadPool(intnThreads){returnnewThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,newLinkedBlockingQueue()); }
corePoolSize与maximumPoolSize相等,即其线程全为核心线程,是一个固定大小的线程池,是其优势;
keepAliveTime = 0 该参数默认对核心线程无效,而FixedThreadPool全部为核心线程;
workQueue 为LinkedBlockingQueue(无界阻塞队列),队列最大值为Integer.MAX_VALUE。如果任务提交速度持续大余任务处理速度,会造成队列大量阻塞。因为队列很大,很有可能在拒绝策略前,内存溢出。是其劣势;
FixedThreadPool的任务执行是无序的;
适用场景:可用于Web服务瞬时削峰,但需注意长时间持续高峰情况造成的队列阻塞。
CachedThreadPool
publicstaticExecutorServicenewCachedThreadPool(){returnnewThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,newSynchronousQueue()); }
corePoolSize = 0,maximumPoolSize = Integer.MAX_VALUE,即线程数量几乎无限制;
keepAliveTime = 60s,线程空闲60s后自动结束。
workQueue 为 SynchronousQueue 同步队列,这个队列类似于一个接力棒,入队出队必须同时传递,因为CachedThreadPool线程创建无限制,不会有队列等待,所以使用SynchronousQueue;
适用场景:快速处理大量耗时较短的任务,如Netty的NIO接受请求时,可使用CachedThreadPool。
SingleThreadExecutor
publicstaticExecutorServicenewSingleThreadExecutor(){returnnewFinalizableDelegatedExecutorService (newThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS,newLinkedBlockingQueue())); }
咋一瞅,不就是newFixedThreadPool(1)吗?定眼一看,这里多了一层FinalizableDelegatedExecutorService包装,这一层有什么用呢,写个dome来解释一下:
publicstaticvoidmain(String[] args){ ExecutorService fixedExecutorService = Executors.newFixedThreadPool(1); ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) fixedExecutorService; System.out.println(threadPoolExecutor.getMaximumPoolSize()); threadPoolExecutor.setCorePoolSize(8); ExecutorService singleExecutorService = Executors.newSingleThreadExecutor();// 运行时异常 java.lang.ClassCastException// ThreadPoolExecutor threadPoolExecutor2 = (ThreadPoolExecutor) singleExecutorService;}
对比可以看出,FixedThreadPool可以向下转型为ThreadPoolExecutor,并对其线程池进行配置,而SingleThreadExecutor被包装后,无法成功向下转型。 因此,SingleThreadExecutor被定以后,无法修改,做到了真正的Single。
ScheduledThreadPool
publicstaticScheduledExecutorServicenewScheduledThreadPool(intcorePoolSize){returnnewScheduledThreadPoolExecutor(corePoolSize); }
newScheduledThreadPool调用的是ScheduledThreadPoolExecutor的构造方法,而ScheduledThreadPoolExecutor继承了ThreadPoolExecutor,构造是还是调用了其父类的构造方法。
publicScheduledThreadPoolExecutor(intcorePoolSize){super(corePoolSize, Integer.MAX_VALUE,0, NANOSECONDS,newDelayedWorkQueue()); }
二、自定义线程池
java线程池自定义的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于自定义线程池怎么写、java线程池自定义的信息别忘了在本站进行查找喔。
发布于:2022-12-28,除非注明,否则均为
原创文章,转载请注明出处。