threadpool

Is there an out-of-the-box thread pool with multiple queues (that ensure serial processing of each queue)?

China☆狼群 提交于 2019-12-18 04:33:35
问题 Among all my tasks, I have some that must be processed serially (they can never run concurrently and they must be processed in order). I achieved that creating a separated thread pool with a single thread for each group of tasks that must be executed serially. It works but I don't have the resources for that. I don't control the number of groups, so I might end up with a ridiculous number of threads running simultaneously. Is there any way I can accomplish that with a single thread pool? Is

Testing PriorityBlockingQueue in ThreadPoolExecutor

喜夏-厌秋 提交于 2019-12-18 04:23:11
问题 I realized my ThreadPoolExecutor with PriorityBlockingQueue like in this example: https://stackoverflow.com/a/12722648/2206775 and wrote a test: PriorityExecutor executorService = (PriorityExecutor) PriorityExecutor.newFixedThreadPool(16); executorService.submit(new Runnable() { @Override public void run() { try { Thread.sleep(1000); Thread.sleep(1000); System.out.println("1"); } catch (InterruptedException e) { e.printStackTrace(); } } }, 1); executorService.submit(new Runnable() { @Override

Stopping all thread in .NET ThreadPool?

半腔热情 提交于 2019-12-18 04:20:13
问题 I am using ThreadPool in .NET to make some web request in the background, and I want to have a "Stop" button to cancel all the threads even if they are in the middle of making a request, so a simple bool wont do the job. How can I do that? 回答1: The correct way to handle this is to have a flag object that you signal. The code running in those threads needs to check that flag periodically to see if it should exit. For instance, a ManualResetEvent object is suitable for this. You could then ask

Python 3: does Pool keep the original order of data passed to map?

人走茶凉 提交于 2019-12-18 03:49:16
问题 I have written a little script to distribute workload between 4 threads and to test whether the results stay ordered (in respect to the order of the input): from multiprocessing import Pool import numpy as np import time import random rows = 16 columns = 1000000 vals = np.arange(rows * columns, dtype=np.int32).reshape(rows, columns) def worker(arr): time.sleep(random.random()) # let the process sleep a random for idx in np.ndindex(arr.shape): # amount of time to ensure that arr[idx] += 1 #

Python multiprocessing pool inside daemon process

人盡茶涼 提交于 2019-12-18 03:43:56
问题 I opened up a question for this problem and did not get a thorough enough answer to solve the issue (most likely due to a lack of rigor in explaining my issues which is what I am attempting to correct): Zombie process in python multiprocessing daemon I am trying to implement a python daemon that uses a pool of workers to executes commands using Popen . I have borrowed the basic daemon from http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/ I have only changed the init

Controlling scheduling priority of python threads?

蓝咒 提交于 2019-12-18 03:04:25
问题 I've written a script that uses two thread pools of ten threads each to pull in data from an API. The thread pool implements this code on ActiveState. Each thread pool is monitoring a Redis database via PubSub for new entries. When a new entry is published, python passes the data to a function that uses python's Subprocess.POpen to execute a PHP shell to do the actual work of calling the API. This system of launching PHP shells is necessary for functionality with my PHP web app, so launching

100 threads TIMED_WAITING in tomcat, causing it to stall as total number of threads crosses 200

社会主义新天地 提交于 2019-12-17 23:25:29
问题 Recently one of our production tomcat server became unresponsive because tomcat's busy threads shot upto 200. When we took thread dump before restarting we got 100 threads in TIMED_WAITING state like these 3 threads: ""http-bio-7007"-exec-241" daemon prio=10 tid=0x00002aaab107b000 nid=0x59df waiting on condition [0x0000000051239000] java.lang.Thread.State: TIMED_WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x0000000580d877d0> (a java.util.concurrent.locks

Running two independent tasks simultaneously using threads

百般思念 提交于 2019-12-17 22:22:33
问题 I've studied lots of tutorials on threads in java but I'm unable to find my answer. My question is: how to run two independent threads simultaneously? My case is: I have two tasks; save some data to the database send a push notification on a mobile device. Since these two tasks are independent I want to execute them simultaneously. I tried using a thread pool with two threads but the problem is that the database tasks finishes quickly but it takes some time to send a push notification.

原理剖析(第 003 篇)ThreadPoolExecutor工作原理分析

瘦欲@ 提交于 2019-12-17 19:48:06
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 原理剖析(第 003 篇)ThreadPoolExecutor工作原理分析 一、大致介绍 1、相信大家都用过线程池,对该类ThreadPoolExecutor应该一点都不陌生了; 2、我们之所以要用到线程池,线程池主要用来解决线程生命周期开销问题和资源不足问题; 3、我们通过对多个任务重用线程以及控制线程池的数目可以有效防止资源不足的情况; 4、本章节就着重和大家分享分析一下JDK8的ThreadPoolExecutor核心类,看看线程池是如何工作的; 二、基本字段方法介绍 2.1 构造器 1、四个构造器: // 构造器一 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); } // 构造器二 public ThreadPoolExecutor(int

003. 线程池应用及实现原理剖析

▼魔方 西西 提交于 2019-12-17 19:33:38
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1. 为什么使用线程池?线程池是不是越多越好? 线程在 java 中是一个对象,更是操作系统的资源,线程创建、销毁需要时间。如果创建时间+销毁时间>执行任务时间就很不合算了。 Java 对象占用堆内存,操作系统线程占用系统内存,根据 jvm 规范,一个线程默认最大栈大小 1 M,这个栈空间是需要从系统内存中分配的。线程过多,会消耗很多的内存。 操作系统需要频繁切换线程上下文(大家都想被运行),影响性能。 线程池的推出,就是为了方便控制线程数量。 2. 线程池原理 - 概念 线程池管理器:用于创建并管理线程池,包括创建线程池,销毁线程池,添加新任务。 工作线程:线程池中线程,在没有任务时处于等待状态,可以循环地执行任务。 任务接口:每个任务必须实现的接口,以供工作线程调度任务的执行,它主要规定了任务的入口,任务执行完后的收尾工作,任务的执行状态等。 任务队列:用于存放没有处理的任务。提供一种缓冲机制。 3. 线程池 API - 接口定义和实现类 类型 名称 描述 接口 Executor 最上层的接口,定义了执行任务的方法 execute 接口 ExecutorService 继承了 Executor 接口,扩展了 Callable、Future、关闭方法 接口 ScheduledExecutorService