How to wait for all threads to finish, using ExecutorService?

I need to execute some amount of tasks 4 at a time, something like this: ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(…) { taskExecutor.execute(new MyTask()); } //…wait for completion somehow How can I get notified once all of them are complete? For now I can’t think about anything better than setting some global task counter and decrease … Read more

NSOperation vs Grand Central Dispatch

I’m learning about concurrent programming for iOS. So far I’ve read about NSOperation/NSOperationQueue and GCD. What are the reasons for using NSOperationQueue over GCD and vice versa? Sounds like both GCD and NSOperationQueue abstract away the explicit creation of NSThreads from the user. However the relationship between the two approaches isn’t clear to me so … Read more

What’s the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

I have a Map which is to be modified by several threads concurrently. There seem to be three different synchronized Map implementations in the Java API: Hashtable Collections.synchronizedMap(Map) ConcurrentHashMap From what I understand, Hashtable is an old implementation (extending the obsolete Dictionary class), which has been adapted later to fit the Map interface. While it … Read more

Why there is no ConcurrentHashSet against ConcurrentHashMap

HashSet is based on HashMap. If we look at HashSet<E> implementation, everything is been managed under HashMap<E,Object>. <E> is used as a key of HashMap. And we know that HashMap is not thread safe. That is why we have ConcurrentHashMap in Java. Based on this, I am confused that why we don’t have a ConcurrentHashSet … Read more

Is JavaScript guaranteed to be single-threaded?

JavaScript is known to be single-threaded in all modern browser implementations, but is that specified in any standard or is it just by tradition? Is it totally safe to assume that JavaScript is always single-threaded? 12 s 12 That’s a good question. I’d love to say “yes”. I can’t. JavaScript is usually considered to have … Read more

When and how should I use a ThreadLocal variable?

When should I use a ThreadLocal variable? How is it used? 26 s 26 One possible (and common) use is when you have some object that is not thread-safe, but you want to avoid synchronizing access to that object (I’m looking at you, SimpleDateFormat). Instead, give each thread its own instance of the object. For … Read more

Collection was modified; enumeration operation may not execute

I can’t get to the bottom of this error, because when the debugger is attached, it does not seem to occur. Collection was modified; enumeration operation may not execute Below is the code. This is a WCF server in a Windows service. The method NotifySubscribers() is called by the service whenever there is a data … Read more