iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java如何自定义线程池中队列
  • 428
分享到

Java如何自定义线程池中队列

2024-04-02 19:04:59 428人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录背景问题分析问题解决总结两个队列的UML关系图SynchronousQueue的定义ArrayBlockingQueue的定义分析jdk源码中关于线程池队列的说明背景 业务交互的

背景

业务交互的过程中涉及到了很多关于SFTP下载的问题,因此在代码中定义了一些线程池,使用中发现了一些问题,

代码类似如下所示:

public class ExecutorTest {
    private static ExecutorService es = new ThreadPoolExecutor(2,
            100, 1000, TimeUnit.MILLISECONDS
            , new ArrayBlockingQueue<>(10));
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            es.submit(new MyThread());
        }
    }
    static class MyThread implements Runnable {
        @Override
        public void run() {
            for (; ; ) {
                System.out.println("Thread name=" + Thread.currentThread().getName());
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

如上面的代码所示,定义了一个初始容量为2,最大容量为100,队列长度为10的线程池,期待的运行结果为:

Thread name=pool-1-thread-1
Thread name=pool-1-thread-2
Thread name=pool-1-thread-3
Thread name=pool-1-thread-4
Thread name=pool-1-thread-5
Thread name=pool-1-thread-6
Thread name=pool-1-thread-7
Thread name=pool-1-thread-8
Thread name=pool-1-thread-9
Thread name=pool-1-thread-10
Thread name=pool-1-thread-3
Thread name=pool-1-thread-5
Thread name=pool-1-thread-2
Thread name=pool-1-thread-1
Thread name=pool-1-thread-4
Thread name=pool-1-thread-10
Thread name=pool-1-thread-7
Thread name=pool-1-thread-6
Thread name=pool-1-thread-9
Thread name=pool-1-thread-8
Thread name=pool-1-thread-3
Thread name=pool-1-thread-4
Thread name=pool-1-thread-1
Thread name=pool-1-thread-5
Thread name=pool-1-thread-2
Thread name=pool-1-thread-8
Thread name=pool-1-thread-6
Thread name=pool-1-thread-7
Thread name=pool-1-thread-9
Thread name=pool-1-thread-10

期待十个线程都可以运行,但实际的执行效果如下:

Thread name=pool-1-thread-1
Thread name=pool-1-thread-2
Thread name=pool-1-thread-2
Thread name=pool-1-thread-1
Thread name=pool-1-thread-1
Thread name=pool-1-thread-2
Thread name=pool-1-thread-1
Thread name=pool-1-thread-2
Thread name=pool-1-thread-2
Thread name=pool-1-thread-1
Thread name=pool-1-thread-2
Thread name=pool-1-thread-1
Thread name=pool-1-thread-2
Thread name=pool-1-thread-1

对比可以看出,用上面的方式定义线程池,最终只有两个线程可以运行,即线程池的初始容量大小。其余线程都被阻塞到了队列ArrayBlockingQueue<>(10)

问题分析

我们知道,Executors框架提供了几种常见的线程池分别为:

  • newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
  • newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
  • newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
  • newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

如果将代码中自定义的线程池改为 :

private static ExecutorService es = Executors.newCachedThreadPool();

运行发现,提交的十个线程都可以运行

Executors.newCachedThreadPool()的源码如下:


public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

通过对比发现,newCachedThreadPool使用的是 SynchronousQueue<>()而我们使用的是ArrayBlockingQueue<>(10) 因此可以很容易的发现问题出在队列上。

问题解决

将ArrayBlockingQueue改为SynchronousQueue 问题解决,代码如下:

public class ExecutorTest {
    private static ExecutorService es = new ThreadPoolExecutor(2,
            100, 1000, TimeUnit.MILLISECONDS
            , new SynchronousQueue<>());
    private static ExecutorService es2 = Executors.newCachedThreadPool();
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            es.submit(new MyThread());
        }
    }
    static class MyThread implements Runnable {
        @Override
        public void run() {
            for (; ; ) {
                System.out.println("Thread name=" + Thread.currentThread().getName());
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

总结

两个队列的UML关系图

从图上我们可以看到,两个队列都继承了AbstractQueue实现了BlockingQueue接口,因此功能应该相似

SynchronousQueue的定义

* <p>Synchronous queues are similar to rendezvous channels used in
* CSP and Ada. They are well suited for handoff designs, in which an
* object running in one thread must sync up with an object running
* in another thread in order to hand it some infORMation, event, or
* task.

SynchronousQueue类似于一个传递通道,只是通过他传递某个元素,并没有任何容量,只有当第一个元素被取走,才能在给队列添加元素。

ArrayBlockingQueue的定义

* A bounded {@linkplain BlockingQueue blocking queue} backed by an
* array.  This queue orders elements FIFO (first-in-first-out).  The
* <em>head</em> of the queue is that element that has been on the
* queue the longest time.  The <em>tail</em> of the queue is that
* element that has been on the queue the shortest time. New elements
* are inserted at the tail of the queue, and the queue retrieval
* operations obtain elements at the head of the queue.

ArrayBlockingQueue从定义来看就是一个普通的队列,先入先出,当队列为空时,获取数据的线程会被阻塞,当队列满时,添加队列的线程会被阻塞,直到队列可用。

分析

从上面队列的定义中可以看出,导致线程池没有按照预期运行的原因不是因为队列的问题,应该是关于线程池在提交任务时,从队列取数据的方式不同导致的。

jdk源码中关于线程池队列的说明

* <dt>Queuing</dt>
*
* <dd>Any {@link BlockingQueue} may be used to transfer and hold
* submitted tasks.  The use of this queue interacts with pool sizing:
*
* <ul>
*
* <li> If fewer than corePoolSize threads are running, the Executor
* always prefers adding a new thread
* rather than queuing.</li>
*
* <li> If corePoolSize or more threads are running, the Executor
* always prefers queuing a request rather than adding a new
* thread.</li>
*
* <li> If a request cannot be queued, a new thread is created unless
* this would exceed maximumPoolSize, in which case, the task will be
* rejected.</li>

从说明中可以看到,如果正在运行的线程数必初始容量corePoolSize小,那么Executor会从创建一个新线程去执行任务,如果正在执行的线程数必corePoolSize大,那么Executor会将新提交的任务放到阻塞队列,除非当队列的个数超过了队列的最大长度maxmiumPooSize。

从源码中找到关于提交任务的方法:

public Future<?> submit(Runnable task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<Void> ftask = newTaskFor(task, null);
    execute(ftask);
    return ftask;
}

从源码中看到 subimit实际上是调用了execute方法

execute方法的源码:

public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    
    int c = ctl.get();
    if (workerCountOf(c) < corePoolSize) {
        if (addWorker(command, true))
            return;
        c = ctl.get();
    }
    if (isRunning(c) && workQueue.offer(command)) {
        int recheck = ctl.get();
        if (! isRunning(recheck) && remove(command))
            reject(command);
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
    else if (!addWorker(command, false))
        reject(command);
}

源码中可以看出,提交任务时,首先会判断正在执行的线程数是否小于corePoolSize,如果条件成立那么会直接创建线程并执行任务。如果条件不成立,且队列没有满,那么将任务放到队列,如果条件不成立但是队列满了,那么同样也新创建线程并执行任务。

到此这篇关于Java如何自定义线程池中队列的文章就介绍到这了,更多相关Java 队列内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java如何自定义线程池中队列

本文链接: https://www.lsjlt.com/news/153755.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java如何自定义线程池中队列
    目录背景问题分析问题解决总结两个队列的UML关系图SynchronousQueue的定义ArrayBlockingQueue的定义分析jdk源码中关于线程池队列的说明背景 业务交互的...
    99+
    2024-04-02
  • Java怎么自定义线程池中队列
    本篇内容介绍了“Java怎么自定义线程池中队列”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!背景业务交互的过程中涉及到了很多关于SFTP下载...
    99+
    2023-07-02
  • Java中线程池自定义如何实现
    这篇“Java中线程池自定义如何实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Java中线程池自定义如何实现”文章吧。线...
    99+
    2023-07-05
  • Java中怎么自定义线程池
    本篇文章给大家分享的是有关Java中怎么自定义线程池,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。Java代码ThreadPoolExecutor  &nb...
    99+
    2023-06-17
  • 怎么在Java中自定义线程池
    这篇文章给大家介绍怎么在Java中自定义线程池,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。【1】接口定义public interface IThreadPool<Job exten...
    99+
    2023-06-06
  • Java多线程 自定义线程池详情
    主要介绍: 1.任务队列 2.拒绝策略(抛出异常、直接丢弃、阻塞、临时队列) 3.init( min ) 4.active 5.max ...
    99+
    2024-04-02
  • 详解Java线程池队列中的延迟队列DelayQueue
    目录DelayQueue延迟队列DelayQueue使用场景DelayQueue属性DelayQueue构造方法实现Delayed接口使用示例DelayQueue总结在阻塞队里中,除...
    99+
    2022-12-08
    Java延迟队列DelayQueue Java延迟队列 Java DelayQueue
  • Java中线程池自定义实现详解
    目录前言线程为什么不能多次调用start方法线程池到底是如何复用的前言 最初使用线程池的时候,网上的文章告诉我说线程池可以线程复用,提高线程的创建效率。从此我的脑海中便为线程池打上了...
    99+
    2023-03-01
    Java线程池自定义 Java线程池
  • 怎么在java项目中自定义线程池
    怎么在java项目中自定义线程池?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。使用线程池时,可以使用 newCachedThreadPool()或者 newFi...
    99+
    2023-05-31
    java 线程池 ava
  • java怎么自定义并发线程池
    要自定义并发线程池,可以使用`ThreadPoolExecutor`类来实现。`ThreadPoolExecutor`是`Execu...
    99+
    2023-10-25
    java
  • Java线程池队列中的延迟队列DelayQueue怎么使用
    今天小编给大家分享一下Java线程池队列中的延迟队列DelayQueue怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧...
    99+
    2023-07-04
  • Java自定义线程池的实现示例
    目录一、Java语言本身也是多线程,回顾Java创建线程方式如下:二、JDK线程池工具类.三、业界知名自定义线程池扩展使用.一、Java语言本身也是多线程,回顾Java创建线程方式如...
    99+
    2024-04-02
  • Java 多线程系列Ⅳ(单例模式+阻塞式队列+定时器+线程池)
    多线程案例 一、设计模式(单例模式+工厂模式)1、单例模式2、工厂模式 二、阻塞式队列1、生产者消费者模型2、阻塞对列在生产者消费者之间的作用3、用标准库阻塞队列实现生产者消费者模型4、模...
    99+
    2023-09-17
    java 单例模式 阻塞队列 定时器 线程池 并发编程
  • Java自定义实现链队列详解
    一、写在前面        数据结构中的队列应该是比较熟悉的了,就是先进先出,因为有序故得名队列,就如同排队嘛,在对尾插入新的节点,在对首删除节点.jdk集合框...
    99+
    2023-05-30
    java 链队列 ava
  • python中如何定义栈、队列及双端队列
    这篇文章给大家分享的是有关python中如何定义栈、队列及双端队列的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1.线性数据结构的定义我们首先学习 4 种简单而强大的数据结构。栈、队列、双端队列和列表都是有序的数...
    99+
    2023-06-22
  • Java多线程(单例模式,阻塞队列,定时器,线程池)详解
    目录1. 单例模式(singleton pattern)1.1 懒汉模式1.2 饿汉模式2 阻塞队列(blocking queue)2.1 阻塞队列2.2 生产者消费者模型2.3 标...
    99+
    2024-04-02
  • python自定义线程池控制线程数量
    1.自定义线程池 import threading import Queue import time queue = Queue.Queue() def put_data_in_queue(): for i in xrang...
    99+
    2023-01-31
    线程 自定义 数量
  • Java 阻塞队列和线程池原理分析
    目录【1】阻塞队列一、什么是阻塞队列?二、阻塞队列有什么用?三、阻塞队列的简单实用【2】Java 线程池一、我们为什么需要Java 线程池?使用它的好处是什么?二、Java中主要提供...
    99+
    2024-04-02
  • 关于dubbo 自定义线程池的问题
    目录初识dubbo一、什么是dubbo?二、为什么要用dubbo前言dubbo线程池dubbo线程池说明自定义线程池代码实现步骤初识dubbo 一、什么是dubbo? Dubbo是阿...
    99+
    2024-04-02
  • 使用Java无界队列的线程池会怎么样
    本篇内容主要讲解“使用Java无界队列的线程池会怎么样”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“使用Java无界队列的线程池会怎么样”吧!(1)背景引入今天跟大家聊一个互联网大厂的Java面...
    99+
    2023-06-04
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作