ConcurrentDictionary Pitfall – Are delegates factories from GetOrAdd and AddOrUpdate synchronized?

Yes, you are right, the user delegates are not synchronized by ConcurrentDictionary. If you need those synchronized it is your responsibility. The MSDN itself says: Also, although all methods of ConcurrentDictionary are thread-safe, not all methods are atomic, specifically GetOrAdd and AddOrUpdate. The user delegate that is passed to these methods is invoked outside of … Read more

Spring @Async limit number of threads

If you are using Spring’s Java-configuration, your config class needs to implements AsyncConfigurer: @Configuration @EnableAsync public class AppConfig implements AsyncConfigurer { […] @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(5); executor.setQueueCapacity(50); executor.setThreadNamePrefix(“MyExecutor-“); executor.initialize(); return executor; } } See @EnableAsync documentation for more details : http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html

CPU Intensive Calculation Examples?

A few easy examples of CPU-intensive tasks: searching for prime numbers (involves lots of BigInteger divisions) calculating large factorials e.g. 2000! ((involves lots of BigInteger multiplications) many Math.tan() calculations (this is interesting because Math.tan is native, so you’re using two call stacks: one for Java calls, the other for C calls.)

Calling TaskCompletionSource.SetResult in a non blocking manner

I’ve discovered that TaskCompletionSource.SetResult(); invokes the code awaiting the task before returning. In my case that result in a deadlock. Yes, I have a blog post documenting this (AFAIK it’s not documented on MSDN). The deadlock happens because of two things: There’s a mixture of async and blocking code (i.e., an async method is calling … Read more

Python – appending to same file from multiple threads

The solution is to write to the file in one thread only. import Queue # or queue in Python 3 import threading class PrintThread(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def printfiles(self, p): for path, dirs, files in os.walk(p): for f in files: print(f, file=output) def run(self): while True: result = self.queue.get() self.printfiles(result) self.queue.task_done() … Read more

Does linux schedule a process or a thread?

The Linux scheduler (on recent Linux kernels, e.g. 3.0 at least) is scheduling schedulable tasks or simply tasks. A task may be : a single-threaded process (e.g. created by fork without any thread library) any thread inside a multi-threaded process (including its main thread), in particular Posix threads (pthreads) kernel tasks, which are started internally … Read more