java.util.concurrent

How to concurrently modify a Vector

若如初见. 提交于 2019-12-11 19:28:26
问题 I have to ensure while iterating the Vector; there is no update on that Vector to avoid ConcurrentModificationException . I can use concurrent collection. But i just want to give a try on Vector. Below is the code I have written. public class TestConcurrentModification1 { Vector a = new Vector(); public static void main(String[] args) { final TestConcurrentModification1 obj = new TestConcurrentModification1(); new Thread(){ public void run(){ for(int i = 0; i < 5; i++){ try { Thread.sleep(1);

The concurrent implementation of a publisher/subscriber pattern

半腔热情 提交于 2019-12-11 13:58:30
问题 I want to implement a variety of of a publisher/subscriber pattern using Java and currently running out of ideas. There is 1 publisher and N subscribers, the publisher publish objects then each subscriber need to process the each of the objects once and only once in the correct order. The publisher and each subscriber run in their own thread. In my original implementation, each subscriber has its own blocking queue, and the publisher put objects into each of the subscriber's queue. This works

Unsafe compareAndSwapInt vs synchronize

£可爱£侵袭症+ 提交于 2019-12-11 12:36:43
问题 I found that almost all high level synchronization abstractions(like Semaphore, CountDownLatch, Exchanger from java.util.concurrent) and concurrent collections are using methods from Unsafe(like compareAndSwapInt method) to define critical section. In the same time I expected that synchronize block or method will be used for this purpose. Could you explain is the Unsafe methods(I mean only methods that could atomically set a value) more efficient than synchronize and why it is so? 回答1: Using

Start/Suspend/Resume a method in Java [duplicate]

孤街浪徒 提交于 2019-12-11 09:43:03
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Start/Suspend/Resume/Suspend … a method invoked by other class I want to implement an Anytime k-NN classifier but I cannot find a way to call the "classify(...)" method for a specific amount of time, suspend it, get the available results before the method was suspended, resume the method for a specific amount of time, suspend it, get the available results before the method was suspended, and so on... Thanks in

why is there a while loop in put() of LinkedBlockingQueue

会有一股神秘感。 提交于 2019-12-11 07:36:51
问题 public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); int c = -1; Node<E> node = new Node<E>(e); final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); try { while (count.get() == capacity) { notFull.await(); } enqueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); } why is there a while loop? All the

ThreadPoolExecutor application does not Finish

怎甘沉沦 提交于 2019-12-11 03:52:48
问题 This super simple application prints "Hello" but does not finish. I see absolutely no reason why this should be. JavaDoc, section finalization, says that A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. tpe is clearly not referenced, that means that thread does not finish. But I don't understand why. Could someone explain? Solution in this case is to call shutdown() at the end of main, but my actual application is more complicated.

How will ReentrantLock object created inside a method's local scope work?

半腔热情 提交于 2019-12-11 01:51:58
问题 The above is a screen print from OCP 7 java se book. page 791. My question is if a new ReentrantLock object is created in a method every time and locked, how would that stop two threads from running the code block in between lock and unlock ? Won't the two threads create a ReentrantLock object each and lock it? I can imagine how this would work if lock object was a instance variable only instantiated once and never changed. (preferrably final ). Am I misunderstanding something? I had already

Java shared condition between classes throws IllegalMonitorStateException: null

混江龙づ霸主 提交于 2019-12-10 18:28:36
问题 I have structure something like this: Lock wrapper - is used to store lock, condition and an object from response public class LockWrapper{ private Lock lock; private Condition myCondition; private MyObject myObject; public LockWrapper(Lock lock, Condition myCondition) { this.lock = lock; this.myCondition = myCondition; } public Condition getMyCondition() { return myCondition; } public MyObject getMyObject() { return myObject; } public void setObject(MyObject myObject) { this.myObject =

Java: How to take static snapshot of ConcurrentHashMap?

99封情书 提交于 2019-12-10 12:54:06
问题 Java doc says that return values of method values() and entrySet() are backed by the map. So changes to the map are reflected in the set and vice versa. I don't want this to happen to my static copy. Essentially, I want lots of concurrent operations to be done on my DS. But for some cases I want to iterate over its static snapshot. I want to iterate over static snapshot, as I am assuming iterating over static snapshot will be faster as compared to a version which is being updated concurrently

ArrayBlockingQueue: concurrent put and take

冷暖自知 提交于 2019-12-10 11:57:22
问题 Why ABQ has not been implemented using the way LinkedBlockingQueue. We can use AtomicInteger to keep Track count in ABQ also the same way LBQ does. We can use the Two Locks for ABQ too. I stumble upon the similar question on SO. ArrayBlockingQueue uses a single lock for insertion and removal but LinkedBlockingQueue uses 2 separate locks But I couldn't understand the answer for that question. I need help in understanding that problem which would arise if we implement ABQ using two locks. If