threadpool

How to reuse threads in .NET 3.5

烈酒焚心 提交于 2019-11-28 19:52:46
I have a subroutine that processes large blocks of information. In order to make use of the entire CPU, it divides the work up into separate threads. After all threads have completed, it finishes. I read that creating and destroying threads uses lots of overhead, so I tried using the threadpool, but that actually runs slower than creating my own threads. How can I create my own threads when the program runs and then keep reusing them? I've seen some people say it can't be done, but the threadpool does it so it must be possible, right? Here is part of the code that launches new threads / uses

Linux简单高并发模型——Epoll + 线程池

主宰稳场 提交于 2019-11-28 18:54:56
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_25425023/article/details/70199133 首先是一个locker.h的文件,封装了信号量、互斥量、条件变量。 在线程池中的任务队列需要互斥量的保护,当任务队列中有任务到达时,需要唤醒一个等待pthread_cond_wait()的线程,线程池停止时,需要唤醒所以的线程,调用的是pthread_cond_broadcast()。 locker.h文件: #ifndef _LOCKER_H_ #define _LOCKER_H_ #include <pthread.h> #include <stdio.h> #include <semaphore.h> /*信号量的类*/ class sem_locker { private: sem_t m_sem; public: //初始化信号量 sem_locker() { if(sem_init(&m_sem, 0, 0) != 0) printf("sem init error\n"); } //销毁信号量 ~sem_locker() { sem_destroy(&m_sem); } //等待信号量 bool wait() { return sem

Pre-initializing a pool of worker threads to reuse connection objects (sockets)

僤鯓⒐⒋嵵緔 提交于 2019-11-28 18:53:43
I need to build a pool of workers in Java where each worker has its own connected socket; when the worker thread runs, it uses the socket but keeps it open to reuse later. We decided on this approach because the overhead associated with creating, connecting, and destroying sockets on an ad-hoc basis required too much overhead, so we need a method by which a pool of workers are pre-initializaed with their socket connection, ready to take on work while keeping the socket resources safe from other threads (sockets are not thread safe), so we need something along these lines... public class

How to wait for a ThreadPoolExecutor to finish

人盡茶涼 提交于 2019-11-28 18:38:36
My Question: How to execute a bunch of threaded objects on a ThreadPoolExecutor and wait for them all to finish before moving on? I'm new to ThreadPoolExecutor. So this code is a test to learn how it works. Right now I don't even fill the BlockingQueue with the objects because I don't understand how to start the queue without calling execute() with another RunnableObject . Anyway, right now I just call awaitTermination() but I think I'm still missing something. Any tips would be great! Thanks. public void testThreadPoolExecutor() throws InterruptedException { int limit = 20; BlockingQueue q =

Why does ScheduledThreadPoolExecutor only accept a fixed number of threads?

半腔热情 提交于 2019-11-28 18:38:35
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 ? 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 load files, they will be loaded slower than if they were being loaded sequentially (at least on a harddisk)

tomcat 6 thread pool for asynchronous processing

南楼画角 提交于 2019-11-28 18:29:01
Short question: Within a Tomcat 6 app - how can I run a (separate) thread-pool? What is the best solution for running a thread pool? Long question: I have a simple need here; Poll a database for some data, while allowing web clients wait for an answer (long poll connections). When this data is available at the database I'll send a reply to the relevant client. Saying so, I prefer not to dive into any framework at the moment ( quartz scheduler maybe?). Therefore, as I conclude, I'll need a thread pool to do the job in the background. So if Thread is what I'm about to use (actually Runnable ),

C: What's the way to make a poolthread with pthreads?

为君一笑 提交于 2019-11-28 17:58:09
I have a queue of jobs and I want to make a pool of 4 threads where I can throw my jobs at. What I am stuck at is in how to make the threads and keep them suspended while there is no work. JOB QUEUE | job1 | job2 | job3 | job4 | .. THREAD POOL | thread1 | thread2 | thread3 | thread4 | To create the threads I have currently at the initialisation point: for (t=0; t<num_of_threads; t++){ pthread_create(&(threads[t]), NULL, doSth2, NULL); } Where num_of_threads=4 and doSth2 is a function with nothing inside. So once I have created the 4 threads and they are done with doSth2, how can I give them

Exception handling in ThreadPools

 ̄綄美尐妖づ 提交于 2019-11-28 17:01:40
问题 I have a ScheduledThreadPoolExecutor that seems to be eating Exceptions. I want my executor service to notify me if a submitted Runnable throws an exception. For example, I'd like the code below to at the very least print the IndexArrayOutOfBoundsException's stackTrace threadPool.scheduleAtFixedRate( new Runnable() { public void run() { int[] array = new array[0]; array[42] = 5; } }, 1000, 1500L, TimeUnit.MILLISECONDS); As a side question. Is there a way to write a general try catch block for

Using Task or async/await in IHttpAsyncHandler

萝らか妹 提交于 2019-11-28 16:52:22
问题 Since the very begining of writing ASP.NET applications when I wanted to add a threading there are 3 simple ways I can accomplish threading within my ASP.NET application : Using the System.Threading.ThreadPool . Using a custom delegate and calling its BeginInvoke method. Using custom threads with the aid of System.Threading.Thread class. The first two methods offer a quick way to fire off worker threads for your application. But unfortunately, they hurt the overall performance of your

Maximum (client request) thread pool size in spring

情到浓时终转凉″ 提交于 2019-11-28 16:49:28
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? 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 value for the server.tomcat.max-threads property in your application.properties or application.yml file. 来源: