java.util.concurrent

Need to manually synchronize the Synchronized list while iteration when it could be avoided?

人盡茶涼 提交于 2019-11-29 19:44:40
问题 My question is about synchronizedList method Collections Class. Javadocs say: It is imperative that the user manually synchronize on the returned list when iterating over it: List list = Collections.synchronizedList(new ArrayList()); ... synchronized(list) { Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } Though manually synchroniziation is not required for other methods. I looked into the source code of Collections class and found

Implementation of BlockingQueue: What are the differences between SynchronousQueue and LinkedBlockingQueue

人走茶凉 提交于 2019-11-29 18:49:39
I see these implementation of BlockingQueue and can't understand the differences between them. My conclusion so far: I won't ever need SynchronousQueue LinkedBlockingQueue ensures FIFO, BlockingQueue must be created with parameter true to make it FIFO SynchronousQueue breaks most collections method (contains, size, etc) So when do I ever need SynchronousQueue ? Is the performance of this implementation better than LinkedBlockingQueue ? To make it more complicated... why does Executors.newCachedThreadPool use SynchronousQueue when the others ( Executors.newSingleThreadExecutor and Executors

Does a HashMap with a getAndWait() method exist? E.g. a BlockingConcurrentHashMap implementation?

帅比萌擦擦* 提交于 2019-11-29 18:45:38
问题 Many threads may populate a HashMap , in some cases I need to wait (block) until an object exists in the HashMap, such as: BlockingConcurrentHashMap map = new BlockingConcurrentHashMap(); Object x = map.getAndWait(key, 1000); //(object_to_get, max_delay_ms) Wondering if such a thing exists already, I hate re-inventing wheels. 回答1: As far as I know, there is no 'Transfer Map' available. Though the creation of one in theory isn't too difficult. public class TransferMap<K,V> implements Map<K,V>{

Why Lock condition await must hold the lock

喜你入骨 提交于 2019-11-29 17:08:05
问题 I am in doubt with that , in Java language, we need to acquire the lock, before we await some condition to be satisfied. For example, int java monitor lock: synchronized(lock){ System.out.println("before lock ..."); lock.wait(); System.out.println("after lock ..."); } or the concurrency utils: Lock lock = new ReentrantLock(); Condition cond = lock.newCondition(); lock.lock(); try{ System.out.println("before condition ..."); cond.await(); System.out.println("after condition ..."); }catch

ForkJoinPool stalls during invokeAll/join

对着背影说爱祢 提交于 2019-11-29 14:41:10
问题 I try to use a ForkJoinPool to parallelize my CPU intensive calculations. My understanding of a ForkJoinPool is, that it continues to work as long as any task is available to be executed. Unfortunately I frequently observed worker threads idling/waiting, thus not all CPU are kept busy. Sometimes I even observed additional worker threads. I did not expect this, as I strictly tried to use non blocking tasks. My observation is very similar to those of ForkJoinPool seems to waste a thread. After

Is there BlockingMap as BlockingQueue in java?

微笑、不失礼 提交于 2019-11-29 11:32:09
问题 I'd like to have one BlockingMap data structure which is very similar to BlockingQueue. The take method of BlockingQueue will wait there until element is available. I'd like the get method of BlockingMap to wait there until the corresponding key is available? Is this kind of data structure available that I can use ? 回答1: I have simply used BlockingQueue<Map.Entry<K,V>> in the past. But recently, I came across this Blocking Map for Java. Haven't used it myself, though. 回答2: I hope this is what

Java ThreadPool usage

烈酒焚心 提交于 2019-11-29 09:35:25
问题 I'm trying to write a multithreaded web crawler. My main entry class has the following code: ExecutorService exec = Executors.newFixedThreadPool(numberOfCrawlers); while(true){ URL url = frontier.get(); if(url == null) return; exec.execute(new URLCrawler(this, url)); } The URLCrawler fetches the specified URL, parses the HTML extracts links from it, and schedules unseen links back to frontier. A frontier is a queue of uncrawled URLs. The problem is how to write the get() method. If the queue

ConcurrentHashMap jdk 8 Uses TreeNodes instead of List .. Why? [closed]

五迷三道 提交于 2019-11-29 05:20:56
Hi i know the workings of ConcurrentHashMap before JDK 8. I also understood the code: it was pretty modular and not very hard to understand. The code of ConcurrentHashMap in JDK 8 has changed a lot from its previous implementations. Because this question was classified as too broad I will now try to be very specific. CHMv8 uses a TreeBin (a variant of RedBlackTree) for buckets rather than a linked list. So my question what is the main advantage of using TreeBin over a linked list? Source code here The main changes are to add ConcurrentHashMap specific implementations of the new Java 8 default

Difference between Executor and ExecutorCompletionservice in java

浪尽此生 提交于 2019-11-29 01:08:27
问题 As the question title itself says what is the difference between Executors and ExecutorCompletionService classes in java? I am new to the Threading,so if any one can explain with a piece of code, that would help a lot. 回答1: Suppose you had a set of tasks A, B, C, D, E and you want to execute each of them asynchronously in an Executor and process the results 1 by 1 as they complete. With an Executor , you would do so like this: List<Future<?>> futures = new ArrayList<Future<?>>(); futures.add

Java多线程三(线程安全的集合及java.util.concurrent包的锁)

依然范特西╮ 提交于 2019-11-28 22:57:01
一、线程安全的集合 JDK1.5之前,可以使用Venctor和Hashtable,也可以由java.util.Collections来创建线程安全的集合,如:Connections.synchronizedSet(Set<T>); Connections.synchronizedList(List<T>);Connections.synchronizedMap(Map<K, V>)等,其简单的原理是每个方法都增加了synchronized来保证线程安全。 JDK1.5之后,提供了java.util.concurrent并发包,它提供的新集合类允许通过在语义中的少量更改来获得更高的并发。 CopyOnWriteArrayList 其中的set、add、remove等方法,都使用了ReentrantLock的lock()来加锁,unlock()来解锁。当增加元素的时候使用Arrays.copyOf()来拷贝副本,在副本上增加元素,然后改变原引用指向副本。 CopyOnWriteArraySet 使用了CopyOnWriteArrayList来存储数据,remove方法调用CopyOnWriteArrayList的remove方法。add方法调用了CopyOnWriteArrayList的addIfAbsent方法,addIfAbsent同样使用了ReentrantLock的lock(