threadpool

ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew

旧时模样 提交于 2019-11-27 00:24:42
what is difference between the below ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew If the above code is called 500 times for some long running task does it mean all the thread pool threads will be taken up? Or will TPL (2nd option) be smart enough to just take up threads less or equal to number of processors? If you're going to start a long-running task with TPL, you should specify TaskCreationOptions.LongRunning , which will mean it doesn't schedule it on the thread-pool. (EDIT: As noted in comments, this is a scheduler-specific decision, and isn't a hard and fast guarantee, but I'd

Thread pool using boost asio

岁酱吖の 提交于 2019-11-27 00:06:47
I am trying to create a limited thread pool class using boost::asio. But I am stuck at one point can some one help me. The only problem is the place where I should decrease counter? code does not work as expected. the problem is I don't know when my thread will finish execution and how I will come to know that it has return to pool #include <boost/asio.hpp> #include <iostream> #include <boost/thread/thread.hpp> #include <boost/bind.hpp> #include <boost/thread/mutex.hpp> #include <stack> using namespace std; using namespace boost; class ThreadPool { static int count; int NoOfThread; thread

Does async(launch::async) in C++11 make thread pools obsolete for avoiding expensive thread creation?

筅森魡賤 提交于 2019-11-26 23:49:40
问题 It is loosely related to this question: Are std::thread pooled in C++11?. Though the question differs, the intention is the same: Question 1: Does it still make sense to use your own (or 3rd-party library) thread pools to avoid expensive thread creation? The conclusion in the other question was that you cannot rely on std::thread to be pooled (it might or it might be not). However, std::async(launch::async) seems to have a much higher chance to be pooled. It don't think that it is forced by

FixedThreadPool vs CachedThreadPool: the lesser of two evils

柔情痞子 提交于 2019-11-26 23:32:40
I have a program that spawns threads (~5-150) which perform a bunch of tasks. Originally, I used a FixedThreadPool because this similar question suggested they were better suited for longer lived tasks and with my very limited knowledge of multithreading, I considered the average life of the threads (several minutes) " long lived ". However, I recently added the capability to spawn additional threads and doing so takes me above the thread limit I set. In this case, would it be better to guess and increase the number threads I can allow or to switch to a CachedThreadPool so I have no wasted

How can I shutdown Spring task executor/scheduler pools before all other beans in the web app are destroyed?

不想你离开。 提交于 2019-11-26 23:25:01
In a Spring web application I have several DAO and service layer beans. One service layer bean has annotated @Async / @Scheduled methods. These methods depend on other (autowired) beans. I have configured two thread pools in XML: <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="2" /> <property name="maxPoolSize" value="5" /> <property name="queueCapacity" value="5" /> <property name="waitForTasksToCompleteOnShutdown" value="true" /> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent

How are Threads allocated to handle Servlet request?

♀尐吖头ヾ 提交于 2019-11-26 23:25:01
Can someone please explain what is thread per request and thread per connection? Which model do servlets work on? How threads are allocated to handle HTTP requests? Is it thread/request or connection? And let's say if I want to perform a time consuming task in my Servlet 's doGet() method asynchronously, I start a new thread using Java executors so that lengthy calculations are done in a separate thread and response is sent right away. Now does that ensure that I have freed the thread which had been processing my HttpServletRequest or is it still being used because a child thread is still

If my interface must return Task what is the best way to have a no-operation implementation?

▼魔方 西西 提交于 2019-11-26 23:21:25
In the code below, due to the interface, the class LazyBar must return a task from it's method (and for arguments sake can't be changed). If LazyBar s implementation is unusual in that it happens to run quickly and synchronously - what is the best way to return a No-Operation task from the method? I have gone with Task.Delay(0) below, however I would like to know if this has any performance side-effects if the function is called a lot (for arguments sake, say hundreds of times a second): Does this syntactic sugar un-wind to something big? Does it start clogging up my application's thread pool?

Java Thread Pool with a Bounded Queue

不问归期 提交于 2019-11-26 22:52:59
问题 I'm using java.util.concurrent 's Executors class to create a fixed thread pool for running request handlers for a web server: static ExecutorService newFixedThreadPool(int nThreads) and the description is: Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue. However, I am looking for thread pool implementation which will do the exact same thing, except with a bounded queue. Is there such an implementation? Or do I need to implement my own wrapper

How to implement PriorityBlockingQueue with ThreadPoolExecutor and custom tasks

天涯浪子 提交于 2019-11-26 22:20:53
问题 I've searched a lot but could not find a solutuion to my problem. I have my own class, BaseTask , that uses a ThreadPoolExecutor to handle tasks. I want task prioritization, but when I try to use a PriorityBlockingQueue I get ClassCastException because the ThreadPoolExecutor wraps my Tasks into a FutureTask object. This obviously makes sense because the FutureTask does not implement Comparable , but how would I go on to solve the priority problem? I've read that you could override newTaskFor(

How to stop an executor thread when Tomcat is stopped?

回眸只為那壹抹淺笑 提交于 2019-11-26 21:28:00
问题 I am using an executor service by creating a fixed number of threads to do a HTTP GET data retrieval. executorService = ExecutorServiceFactory.getInstance().getExecutorService(Config.getInstance().getNumberOfThreads(), "ThreadExecutor_"+UIDFactory.getInstance().createSessionID()); executorService.execute(new Retrieve(data)); private class Retrieve implements Runnable{ private Vector<String> data; public WADORetrieve(Vector<String> data) { this.data = data; } @Override public void run() { for