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
Tag: concurrency
What are the main uses of yield(), and how does it differ from join() and interrupt()?
I am a little bit confused about the use of Thread.yield() method in Java, specifically in the example code below. I’ve also read that yield() is ‘used to prevent execution of a thread’. My questions are: I believe the code below result in the same output both when using yield() and when not using it. Is this correct? What are,
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
wait() and notify() method , always IllegalMonitorStateException is happen and tell me current Thread is not Owner Why?
Answer You need to hold the lock on the object you want to wait on (you can only call it within a synchronized block). Also, calling wait on a Thread is very unusual and probably not what you want. I am not sure what you are trying to do, but could you be confusing wait with sleep? If you want
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()?
What is the difference between Thread.start() and Thread.run()?
Why do we call the start() method, which in turn calls the run() method? Can’t we directly make a call to run()? Please give an example where there is a difference.
Java long running task Thread interrupt vs cancel flag
I have a long running task, something like: The task can be cancelled (a cancel is requested and checkIfCancelRequested() checks the cancel flag). Generally when I write cancellable loops like this, I use a flag to indicate that a cancel has been requested. But, I know I could also use Thread.interrupt and check if the thread has been interrupted. I’m
Impossible to make a cached thread pool with a size limit?
It seems to be impossible to make a cached thread pool with a limit to the number of threads that it can create. Here is how static Executors.newCachedThreadPool is implemented in the standard Java library: So, using that template to go on to create a fixed sized cached thread pool: Now if you use this and submit 3 tasks, everything
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