java.util.concurrent

Are read and write locks in ReentrantReadWriteLock somehow related?

不羁的心 提交于 2019-12-22 06:27:21
问题 Please explain me more the contract. I can't figure out if two locks contained in ReentrantReadWriteLock somehow related? Or these are just a bundle of two normal locks? 回答1: If threads are waiting Read Lock it is shared but when thread wants to acquire write lock only that thread is allowed the access same as mutual exclusion. So either one of operation is allowed . if lock is held by readers and thread request write lock no more readers are allowed to acquire read lock until thread which

Java Remove Specific Item From ConcurrentHashMap

馋奶兔 提交于 2019-12-22 04:04:30
问题 Is using the remove() method okay? I've read an article that synchronization hasn't been added to the remove method. How do I properly remove a specific item from a ConcurrentHashMap? Example Code: ConcurrentHashMap<String,Integer> storage = new ConcurrentHashMap<String,Integer>(); storage.put("First", 1); storage.put("Second", 2); storage.put("Third",3); //Is this the proper way of removing a specific item from a tread-safe collection? storage.remove("First"); for (Entry<String, Integer>

Is it not possible to supply a thread facory or name pattern to ForkJoinPool?

我的未来我决定 提交于 2019-12-22 04:02:39
问题 I would like to set name for threads of the ForkJoinPool used by work stealing pool, supplied by ExecutorService newWorkStealingPool(int parallelism) or ExecutorService newWorkStealingPool() So far I could not find a way to set custom names on threads used by this ExecutorService , is there a way? newWorkStealingPool() basically supplies a ForkJoinPool , but ForkJoinPool also doesn't have a public constructor with supplied name pattern. update : I have now found this constructor of

AtomicInteger.incrementAndGet() vs. AtomicInteger.getAndIncrement()

可紊 提交于 2019-12-22 01:23:10
问题 When return value is not of interest, is there any (even irrelevant in practice) difference between AtomicInteger.getAndIncrement() and AtomicInteger.incrementAndGet() methods, when return value is ignored? I'm thinking of differences like which would be more idiomatic, as well as which would put less load in CPU caches getting synchronized, or anything else really, anything to help decide which one to use more rationally than tossing a coin. 回答1: The code is essentially the same so it does

Directory Scanner in Java

雨燕双飞 提交于 2019-12-21 20:37:23
问题 Scan a set of directories continuously for a set of file name filters. For each file name filter arrived, process the file and repeat the steps for all What can be the recommended design for this in jdk 1.5 , possibly using java.concurrent.Executor and Future 回答1: I have done a similar task with the web crawler.Just a few changes had to be made... It is a concurrent implementation with newly found directories getting scanned by the thread pool in the Executor Framework.It uses concurrent

Can you interrupt BufferedReader.readLine() with Future.cancel(true)?

六眼飞鱼酱① 提交于 2019-12-21 12:56:20
问题 Let's say I started a thread and I have something like this: ...//initiate all the socket connection future = executor.submit ( new Runnable() { public void run() { ... ... while ((str = in.readLine()) != null) { //do something here } } ); executor is a ExecutorService object and in is a BufferedReader object I know you can close the socket from a different thread to interrupt this thread. But when I try to use future.cancel(true) method, even though it returns true, the thread seems still

ThreadPoolExecutor vs ForkJoinPool: stealing subtasks

北城以北 提交于 2019-12-21 12:05:42
问题 From java docs, A ForkJoinPool differs from other kinds of ExecutorService mainly by virtue of employing work-stealing: all threads in the pool attempt to find and execute subtasks created by other active tasks (eventually blocking waiting for work if none exist). This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTasks). When setting asyncMode to true in constructors, ForkJoinPools may also be appropriate for use with event-style tasks that are never

Why are ConcurrentSkipListSet ascending Iterators 'faster' than descending ones?

情到浓时终转凉″ 提交于 2019-12-21 07:25:12
问题 I’m using the descendingIterator method on ConcurrentSkipListSet. I’ve just checked the documentation and noticed the following comment: ‘Ascending ordered views and their iterators are faster than descending ones.’ See https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#descendingIterator-- Unfortunately it doesn’t provide any more information on this. What kind of performance difference is there? is it significant? and why is there a performance

ConcurrentHashMap.newKeySet() vs Collections.newSetFromMap()

拟墨画扇 提交于 2019-12-20 11:52:57
问题 Java 8 introduced new way to obtain a concurrent Set implementation // Pre-Java-8 way to create a concurrent set Set<String> oldStyle = Collections.newSetFromMap(new ConcurrentHashMap<>()); // New method in Java 8 Set<String> newStyle = ConcurrentHashMap.newKeySet(); Is there any reason to prefer new method? Any advantages/disadvantages? 回答1: ConcurrentHashMap.newKeySet() should be somewhat more efficient as removes a single level of indirection. Collections.newSetFromMap(map) is mostly based

When is CopyOnWriteArraySet useful to achieve thread-safe HashSet?

荒凉一梦 提交于 2019-12-20 09:37:53
问题 In Java , there is thread-safe version HashMap named ConcurrentHashMap and thread-safe version TreeMap named ConcurrentSkipListMap, but there is no ConcurrentHashSet for HashSet. Instead, there are usually 4 ways to use thread-safe Set : Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); Set<String> s = Collections.synchronizedSet(new HashSet<String>()); ConcurrentSkipListSet<E> CopyOnWriteArraySet<E> 1 use keySet() of ConcurrentHashMap to achieve both