Skip to content
Advertisement

Tag: concurrency

Why is this ArrayList throwing a ConcurrentModificationException when I try to remove an element?

I’m trying to remove a particular element from Arraylist, it throws an ConcurrentModificationException any comments, what am I doing wrong? Answer Only remove an element from the array while iterating by using Iterator.remove(). The line for(String st: ar) { is a bit misleading. You’re actually creating an iterator behind the scenes which is being used for this iteration. If you

Does any JVM implement blocking with spin-waiting?

In Java Concurrency in Practice, the authors write: When locking is contended, the losing thread(s) must block. The JVM can implement blocking either via spin-waiting (repeatedly trying to acquire the lock until it succeeds) or by suspending the blocked thread through the operating system. Which is more efficient depends on the relationship between context switch overhead and the time until

Why do threads share the heap space?

Threads each have their own stack, but they share a common heap. Its clear to everyone that stack is for local/method variables & heap is for instance/class variables. What is the benefit of sharing heap among threads. There are several number of threads running simultaneously, so sharing memory can lead to issues such as concurrent modification, mutual exclusion etc overhead.

Why must wait() always be in synchronized block

We all know that in order to invoke Object.wait(), this call must be placed in synchronized block, otherwise an IllegalMonitorStateException is thrown. But what’s the reason for making this restriction? I know that wait() releases the monitor, but why do we need to explicitly acquire the monitor by making particular block synchronized and then release the monitor by calling wait()?

Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource utilization? Answer I think the docs explain the difference and usage of these two functions pretty well: newFixedThreadPool Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads

Advertisement