Naming threads and thread-pools of ExecutorService
Guava almost always has what you need. ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat(“my-sad-thread-%d”).build() and pass it off to your ExecutorService.
Guava almost always has what you need. ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat(“my-sad-thread-%d”).build() and pass it off to your ExecutorService.
I’d let the Framework manage the threading and wouldn’t create any extra threads, unless profiling tests suggest I might need to. Especially, if the calls inside HandleConnectionAsync are mostly IO-bound. Anyway, if you like to release the calling thread (the dispatcher) at the beginning of HandleConnectionAsync, there’s a very easy solution. You can jump on … Read more
The process is pretty simple. First create an asio::io_service and a thread_group. Fill the thread_group with threads linked to the io_service. Assign tasks to the threads using the boost::bind function. To stop the threads (usually when you are exiting your program) just stop the io_service and join all threads. You should only need these headers: … Read more
This is adapted from my answer to another very similar post. Let’s build a ThreadPool class: class ThreadPool { public: void Start(); void QueueJob(const std::function<void()>& job); void Stop(); void busy(); private: void ThreadLoop(); bool should_terminate = false; // Tells threads to stop looking for jobs std::mutex queue_mutex; // Prevents data races to the job queue … Read more
Some people would say that two threads is too many – I’m not quite in that camp 🙂 Here’s my advice: measure, don’t guess. One suggestion is to make it configurable and initially set it to 100, then release your software to the wild and monitor what happens. If your thread usage peaks at 3, … Read more
Thread pool will provide benefits for frequent and relatively short operations by Reusing threads that have already been created instead of creating new ones (an expensive process) Throttling the rate of thread creation when there is a burst of requests for new work items (I believe this is only in .NET 3.5) If you queue … Read more
The simplest approach is to use ExecutorService.invokeAll() which does what you want in a one-liner. In your parlance, you’ll need to modify or wrap ComputeDTask to implement Callable<>, which can give you quite a bit more flexibility. Probably in your app there is a meaningful implementation of Callable.call(), but here’s a way to wrap it … Read more