threadpool

Java Concurrent Object Pool?

孤街醉人 提交于 2019-12-01 07:12:19
问题 I tried to integrate an external non-thread-safe library to my web project; I found out that it's too expensive to create an instance of this object for each client thread. As a result, I would like to create an object pool which has the following property. Dynamic object creation, the objects in the pool are dynamically created instead of creating them in the constructor. The pool is initially empty, and when a client thread acquires a resource object, the pool can create a new resource on

Using Task.Yield to overcome ThreadPool starvation while implementing producer/consumer pattern

谁都会走 提交于 2019-12-01 06:29:10
Answering the question: Task.Yield - real usages? I proposed to use Task.Yield allowing a pool thread to be reused by other tasks. In such pattern: CancellationTokenSource cts; void Start() { cts = new CancellationTokenSource(); // run async operation var task = Task.Run(() => SomeWork(cts.Token), cts.Token); // wait for completion // after the completion handle the result/ cancellation/ errors } async Task<int> SomeWork(CancellationToken cancellationToken) { int result = 0; bool loopAgain = true; while (loopAgain) { // do something ... means a substantial work or a micro batch here - not

Unexpected behaviour for ThreadPool.QueueUserWorkItem

孤者浪人 提交于 2019-12-01 05:16:08
Please check the code sample below: public class Sample { public int counter { get; set; } public string ID; public void RunCount() { for (int i = 0; i < counter; i++) { Thread.Sleep(1000); Console.WriteLine(this.ID + " : " + i.ToString()); } } } class Test { static void Main() { Sample[] arrSample = new Sample[4]; for (int i = 0; i < arrSample.Length; i++) { arrSample[i] = new Sample(); arrSample[i].ID = "Sample-" + i.ToString(); arrSample[i].counter = 10; } foreach (Sample s in arrSample) { ThreadPool.QueueUserWorkItem(callback => s.RunCount()); } Console.ReadKey(); } } The expected output

聊聊HystrixPropertiesStrategy

China☆狼群 提交于 2019-12-01 05:01:56
序 本文主要研究一下HystrixPropertiesStrategy HystrixPropertiesStrategy hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/strategy/properties/HystrixPropertiesStrategy.java /** * Abstract class with default implementations of factory methods for properties used by various components of Hystrix. * <p> * See {@link HystrixPlugins} or the Hystrix GitHub Wiki for information on configuring plugins: <a * href="https://github.com/Netflix/Hystrix/wiki/Plugins">https://github.com/Netflix/Hystrix/wiki/Plugins</a>. */ public abstract class HystrixPropertiesStrategy { /** * Construct an implementation of {@link

How Can I determine the number of items in ThreadPool Queue

≯℡__Kan透↙ 提交于 2019-12-01 04:18:31
I am using the ThreadPool to queue 1000's of workitems While(reading in data for processing) { args = some data that has been read; ThreadPool.QueueUserWorkItem(new WaitCallback(threadRunner), args); } This is working very well, however, as the main thread queues the requests faster than they are processed memory is slowly eaten up. I would like to do something akin to the following to throttle the queueing as the queue grows Thread.Sleep(numberOfItemsCurrentlyQueued); This would result in longer waits as the queue grows. Is there any way to discover how many items are in the queue? I don't

Unexpected behaviour for ThreadPool.QueueUserWorkItem

≡放荡痞女 提交于 2019-12-01 01:59:15
问题 Please check the code sample below: public class Sample { public int counter { get; set; } public string ID; public void RunCount() { for (int i = 0; i < counter; i++) { Thread.Sleep(1000); Console.WriteLine(this.ID + " : " + i.ToString()); } } } class Test { static void Main() { Sample[] arrSample = new Sample[4]; for (int i = 0; i < arrSample.Length; i++) { arrSample[i] = new Sample(); arrSample[i].ID = "Sample-" + i.ToString(); arrSample[i].counter = 10; } foreach (Sample s in arrSample) {

聊聊HystrixThreadPool

女生的网名这么多〃 提交于 2019-12-01 01:13:40
序 本文主要研究一下HystrixThreadPool HystrixThreadPool hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/HystrixThreadPool.java /** * ThreadPool used to executed {@link HystrixCommand#run()} on separate threads when configured to do so with {@link HystrixCommandProperties#executionIsolationStrategy()}. * <p> * Typically each {@link HystrixCommandGroupKey} has its own thread-pool so that any one group of commands can not starve others from being able to run. * <p> * A {@link HystrixCommand} can be configured with a thread-pool explicitly by injecting a {@link HystrixThreadPoolKey} or via the * {@link

Detecting that a ThreadPool WorkItem has completed/waiting for completion

Deadly 提交于 2019-11-30 22:58:13
For whatever reason, ThreadPool 's QueueWorkItem doesn't return an IAsyncResult or some other handle to the work item, which would allow to wait until it's completed. There are RegisterWait... methods, but you have to pass a WaitHandle and creating them is expensive (see IAsyncResult documentation, which advises you to delay creating a WaitHandle until requested). The Task Parallel Library will fix this lack, but there is a long wait before that's available. So, are there any problems with this design: public class Concurrent<T> { private ManualResetEvent _resetEvent; private T _result; public

Twisted: Creating a ThreadPool and then daemonizing leads to uninformative hangs

戏子无情 提交于 2019-11-30 20:59:46
I am developing a networked application in Twisted, part of which consists of a web interface written in Django. I wish to use Twisted's WSGI server to host the web interface, and I've written a working "tap" plugin to allow me to use twistd . When running the server with the -n flag (don't daemonize) everything works fine, but when this flag is removed the server doesn't respond to requests at all, and there are no messages logged (though the server is still running). There is a bug on Twisted's Trac which seems to describe the problem exactly, and my plugin happens to be based on the code

Async await and threads [duplicate]

荒凉一梦 提交于 2019-11-30 20:48:12
This question already has an answer here: async - stay on the current thread? 3 answers I am working with async-await and tasks, but I can't understand the one thing: Is async task executes in separate thread? As msdn says ( Asynchronous programming ): The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. But in the remarks in description of ThreadPool class ( ThreadPool Class ): Examples of operations that use thread pool threads include the following: When you create a Task