threadpool

Using boost::asio thread pool for general purpose tasks

大城市里の小女人 提交于 2019-11-27 20:17:33
In this blog I found a pretty neat example on how to create a simple thread pool using boost::asio. I basically want to use it like this: #include <thread> #include <functional> #include <boost/asio.hpp> int main ( int argc, char* argv[] ) { asio::io_service io_service; asio::io_service::work work(io_service); std::vector<std::thread> threadPool; for(size_t t = 0; t < std::thread::hardware_concurrency(); t++){ threadPool.push_back(thread(std::bind(&asio::io_service::run, &io_service))); } io_service.post(std::bind(an_expensive_calculation, 42)); io_service.post(std::bind(a_long_running_task,

Why does ScheduledThreadPoolExecutor only accept a fixed number of threads?

故事扮演 提交于 2019-11-27 20:17:11
问题 I may imagine some tasks scheduled to take a very long time and ScheduledThreadPoolExecutor would create additional threads for the other tasks that need to be run, until a maximum number of threads is reached. But seems that I can only specify a fixed number of threads for the pool, why is that so ? 回答1: As the why, I don't know either. But I can imagine. The amount of resources of a computer is limited. Not all resources can be handled concurrently either. If multiple processes concurrently

Is it true that for long running processes it is better to do thread manually instead of threadpool?

こ雲淡風輕ζ 提交于 2019-11-27 20:10:37
I read on the other day that for long-running tasks my best bet is to manually create threads instead of using .NET’s thread pool or Task Parallel. I'd really like someone to enlighten me as I am learning about c# threading, specially for long running IO tasks. Thank you in advance. Nicholas Butler That is true. The thread pool is optimised for small units of work and you can interfere with other work by holding onto a thread pool thread. My rule of thumb is if an operation can take more than a second, it should not be on a thread pool thread. That is probably quite long. Although this is

Java Thread Pool with a Bounded Queue

做~自己de王妃 提交于 2019-11-27 20:04:01
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 for the fixed thread pool? lscoughlin What you want to do is new your own ExecutorService, probably

Maximum (client request) thread pool size in spring

杀马特。学长 韩版系。学妹 提交于 2019-11-27 19:59:05
问题 I am developing application server using spring boot app but now I want to know what is the default maximum (client request) thread pool size in spring and how can I customize that value? 回答1: Assuming that you're using embedded Tomcat, Spring Boot uses the server.tomcat.max-threads property to control the size of the client request thread pool. Its default value is zero which leaves Tomcat to use its default of 200. To customise the size of this thread pool you should specify a non-zero

Java `OutOfMemoryError` when creating < 100 threads

╄→гoц情女王★ 提交于 2019-11-27 17:56:02
问题 I've been reading and testing and banging my head on the wall for over a day because of this error. I have some Java code in a class called Listener that looks like this ExecutorService executor = Executors.newFixedThreadPool(NTHREADS); boolean listening = true; int count = 0; while (listening) { Runnable worker; try { worker = new ServerThread(serverSocket.accept()); // this is line 254 executor.execute(worker); count++; logger.info("{} threads started", count); } catch (Exception e1){ //...

Propagating ThreadLocal to a new Thread fetched from a ExecutorService

给你一囗甜甜゛ 提交于 2019-11-27 17:51:51
I'm running a process in a separate thread with a timeout, using an ExecutorService and a Future (example code here ) (the thread "spawning" takes place in a AOP Aspect). Now, the main thread is a Resteasy request. Resteasy uses one ore more ThreadLocal variables to store some context information that I need to retrieve at some point in my Rest method call. Problem is, since the Resteasy thread is running in a new thread, the ThreadLocal variables are lost. What would be the best way to "propagate" whatever ThreadLocal variable is used by Resteasy to the new thread? It seems that Resteasy uses

What's the difference between ThreadPool vs Pool in Python multiprocessing module

余生颓废 提交于 2019-11-27 17:49:00
Whats the difference between ThreadPool and Pool in multiprocessing module. When I try my code out, this is the main difference I see: from multiprocessing import Pool import os, time print("hi outside of main()") def hello(x): print("inside hello()") print("Proccess id: ", os.getpid()) time.sleep(3) return x*x if __name__ == "__main__": p = Pool(5) pool_output = p.map(hello, range(3)) print(pool_output) I see the following output: hi outside of main() hi outside of main() hi outside of main() hi outside of main() hi outside of main() hi outside of main() inside hello() Proccess id: 13268

What is the use of a Thread pool in Java?

牧云@^-^@ 提交于 2019-11-27 17:15:31
What is the use of a Thread pool? Is there a good real world example? Amir Rachum A thread pool is a group of threads initially created that waits for jobs and executes them. The idea is to have the threads always existing, so that we won't have to pay overhead time for creating them every time. They are appropriate when we know there's a stream of jobs to process, even though there could be some time when there are no jobs. Here's a nice diagram from Wikipedia : Justin Ethier Thread Pools from the Java Tutorials has a good overview: Using worker threads minimizes the overhead due to thread

be notified when all background threadpool threads are finished

梦想与她 提交于 2019-11-27 16:32:26
问题 I have a scenario when I start 3..10 threads with ThreadPool. Each thread does its job and returns to the ThreadPool. What are possible options to be notified in main thread when all background threads have finished? Currently I'm using a homegrown method with incrementing a variable for each of created threads and decrementing it when a background thread is about to finish. This works just fine, but I was curious if there are better options. 回答1: Decrementing a variable (between threads) is