threadpool

ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method

筅森魡賤 提交于 2019-11-30 04:35:44
Passing two parameters to a new thread on the threadpool can sometimes be complicated, but it appears that with lambda expressions and anonymous methods, I can do this: public class TestClass { public void DoWork(string s1, string s2) { Console.WriteLine(s1); Console.WriteLine(s2); } } try { TestClass test = new TestClass(); string s1 = "Hello"; string s2 = "World"; ThreadPool.QueueUserWorkItem( o => test.DoWork(s1, s2) ); } catch (Exception ex) { //exception logic } Now, I've certainly simplified this example, but these points are key: The string objects being passed are immutable and

Thread pool that binds tasks for a given ID to the same thread

落爺英雄遲暮 提交于 2019-11-30 03:29:13
Are there any implementations of a thread pool (in Java) that ensures all tasks for the same logical ID are executed on the same thread? The logic I'm after is if there is already a task being executed on a specific thread for a given logical ID, then new tasks with the same ID are scheduled on the same thread. If there are no threads executing a task for the same ID then any thread can be used. This would allow tasks for unrelated IDs to be executed in parallel, but tasks for the same ID to be executed in serial and in the order submitted. If not, are there any suggestions on how I might

Ninject - In what scope DbContext should get binded when RequestScope is meaningless?

China☆狼群 提交于 2019-11-30 01:37:28
问题 In an MVC / WebAPI environment I would use InRequestScope to bind the DbContext . However, I am now on a Console application / Windows service / Azure worker role (doesn't really matter, just there's no Web request scope), which periodically creates a number of Tasks that run asynchronously. I would like each task to have its own DbContext , and since tasks run on their own thread, I tried binding DbContext using InThreadScope . Unfortunately, I realize that the DbContext is not disposed when

How to force Task.Factory.StartNew to a background thread?

纵然是瞬间 提交于 2019-11-30 01:24:50
问题 I have seen numerous other questions similar to this but did not find my answer there. My problem was that I was creating threads with the following flow: private void btn_Click(object sender, EventArgs e) { service.GetCount( (count, ex) => { if (ex != null) return; for (int i = 0; i < count; i++) { service.Get(onItemReceived, i); } } ); } public void GetCount(Action<int, Exception> callback) { var callingThread = TaskScheduler.FromCurrentSynchronizationContext(); Func<int> action = () => {

咱们来聊聊并发工具类Semaphore

房东的猫 提交于 2019-11-29 23:51:34
什么是 Semaphore ? Semaphore是计数信号量。Semaphore管理一系列许可证。每个acquire方法阻塞,直到有一个许可证可以获得然后拿走一个许可证;每个release方法增加一个许可证,这可能会释放一个阻塞的acquire方法。然而,其实并没有实际的许可证这个对象,Semaphore只是维持了一个可获得许可证的数量。 应用场景 Semaphore可以用于做流量控制,特别公用资源有限的应用场景,比如数据库连接。假如有一个需求,要读取几万个文件的数据,因为都是IO密集型任务,我们可以启动几十个线程并发的读取,但是如果读到内存后,还需要存储到数据库中,而数据库的连接数只有10个,这时我们必须控制只有十个线程同时获取数据库连接保存数据,否则会报错无法获取数据库连接。这个时候,我们就可以使用Semaphore来做流控,代码如下: package org.java.base.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class SemaphoreTest { private static final int THREAD_COUNT =

聊聊HystrixPropertiesStrategy HystrixPropertiesChainedProperty

主宰稳场 提交于 2019-11-29 23:44:13
本文主要研究一下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

c# Threadpool - limit number of threads

自闭症网瘾萝莉.ら 提交于 2019-11-29 23:07:55
I am developing a console app. I want to use a Threadpool to perform web downloads. Here is some fake code. for (int loop=0; loop< 100; loop++) { ThreadPool.QueueUserWorkItem(new WaitCallback(GetPage), pageList[loop]); } snip private static void GetPage(object o) { //get the page } How do I prevent my code from starting more than two (or ten, or whatever) simultaneous threads? I have tried ThreadPool.SetMaxThreads(1, 0); ThreadPool.SetMinThreads(1, 0); But they seem to have no impact. I would use Parallel.For and set MaxDegreeOfParallelism accordingly. Parallel.For(0, 1000, new ParallelOptions

Difference between delegate.BeginInvoke and using ThreadPool threads in C#

谁说胖子不能爱 提交于 2019-11-29 20:56:57
In C# is there any difference between using a delegate to do some work asynchronously (calling BeginInvoke()) and using a ThreadPool thread as shown below public void asynchronousWork(object num) { //asynchronous work to be done Console.WriteLine(num); } public void test() { Action<object> myCustomDelegate = this.asynchronousWork; int x = 7; //Using Delegate myCustomDelegate.BeginInvoke(7, null, null); //Using Threadpool ThreadPool.QueueUserWorkItem(new WaitCallback(asynchronousWork), 7); Thread.Sleep(2000); } Edit: BeginInvoke makes sure that a thread from the thread pool is used to execute

What's the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool?

倖福魔咒の 提交于 2019-11-29 20:09:53
Java 5 has introduced support for asynchronous task execution by a thread pool in the form of the Executor framework, whose heart is the thread pool implemented by java.util.concurrent.ThreadPoolExecutor. Java 7 has added an alternative thread pool in the form of java.util.concurrent.ForkJoinPool. Looking at their respective API, ForkJoinPool provides a superset of ThreadPoolExecutor's functionality in standard scenarios (though strictly speaking ThreadPoolExecutor offers more opportunities for tuning than ForkJoinPool). Adding to this the observation that fork/join tasks seem to be faster

C# - When to use standard threads, ThreadPool, and TPL in a high-activity server

ⅰ亾dé卋堺 提交于 2019-11-29 19:53:45
I've been reading a lot about threading lately as I am looking to develop a high-performance, scalable TCP server capable of handling up to 10,000-20,000 clients, each client of which is consistently communicating bidirectionally to the server with a command-based system. The server will receive a command, and execute either a single (or many) tasks as per the command. My question is how to appropriately make use of the .NET threading constructs for a variety of situations, executing tasks that could take between one minute to several hours, depending on the work being performed. What's