java.util.concurrent

Need simple explanation how “lock striping” works with ConcurrentHashMap

假装没事ソ 提交于 2019-11-28 18:20:20
According to Java Concurrency in Practice, chapter 11.4.3 says: Lock splitting can sometimes be extended to partition locking on a variablesized set of independent objects, in which case it is called lock striping. For example, the implementation of ConcurrentHashMap uses an array of 16 locks, each of which guards 1/16 of the hash buckets; bucket N is guarded by lock N mod 16. I still have problems to understand and visualize the lock striping and buckets mechanism. Can someone explain this with good understanding words :) Thanks in advance. The hash map is built on an array, where the hash

Java 8: Parallel FOR loop

时间秒杀一切 提交于 2019-11-28 18:06:15
I have heard Java 8 provides a lot of utilities regarding concurrent computing. Therefore I am wondering what is the simplest way to parallelise the given for loop? public static void main(String[] args) { Set<Server> servers = getServers(); Map<String, String> serverData = new ConcurrentHashMap<>(); for (Server server : servers) { String serverId = server.getIdentifier(); String data = server.fetchData(); serverData.put(serverId, data); } } Read up on streams , they're all the new rage. Pay especially close attention to the bit about parallelism: "Processing elements with an explicit for-loop

Can I use the work-stealing behaviour of ForkJoinPool to avoid a thread starvation deadlock?

人走茶凉 提交于 2019-11-28 15:54:53
问题 A thread starvation deadlock occurs in a normal thread pool if all the threads in the pool are waiting for queued tasks in the same pool to complete. ForkJoinPool avoids this problem by stealing work from other threads from inside the join() call, rather than simply waiting. For example: private static class ForkableTask extends RecursiveTask<Integer> { private final CyclicBarrier barrier; ForkableTask(CyclicBarrier barrier) { this.barrier = barrier; } @Override protected Integer compute() {

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

不问归期 提交于 2019-11-28 13:45:41
问题 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

Whether to use invokeAll or submit - java Executor service

谁都会走 提交于 2019-11-28 07:43:50
I have a scenario where I have to execute 5 thread asynchronously for the same callable. As far as I understand, there are two options: 1) using submit(Callable) ExecutorService executorService = Executors.newFixedThreadPool(5); List<Future<String>> futures = new ArrayList<>(); for(Callable callableItem: myCallableList){ futures.add(executorService.submit(callableItem)); } 2) using invokeAll(Collections of Callable) ExecutorService executorService = Executors.newFixedThreadPool(5); List<Future<String>> futures = executorService.invokeAll(myCallableList)); What should be the preferred way? Is

Is there java.util.concurrent equivalent for WeakHashMap?

情到浓时终转凉″ 提交于 2019-11-28 06:10:37
Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency? Collections.synchronizedMap(new WeakHashMap<Class, Object>()); i.e. is there something from java.util.concurrent one can use instead? Note that merely replacing with new ConcurrentHashMap<Class, Object>(new WeakHashMap<Class, Object>())); obviously won't work Steven Schlansker Guava 's CacheBuilder class allows you to do this easily. CacheBuilder.newBuilder().weakKeys().build() Note that this changes key equality semantics to be == instead of .equals() which will not

Why does this code result in illegalMonitorState exception?

大憨熊 提交于 2019-11-28 05:58:38
问题 The code below is trying to insert a random value into a circular queue and remove it. However, there are some synchronization issues. I know I can use higher level routines and I'm going to do that for production code but I was curious why this does not work ? What am I missing here ? public class CircularQueue { int count; int rear; int front; Object lock = new Object(); int size; int[] array; CircularQueue(int size) { this.size= size; array = new int[size]; } void enqueue(int number)

@GuardedBy , @ThreadSafe ,@NotThreadSafe

橙三吉。 提交于 2019-11-28 05:15:35
I see that the above annotations are used extensively in the book JCIP . I think it is really useful because even in the absence of proper documentation it says some things about the synchronization policies . I also see that Intellij Idea makes use of these annotations Are they now actually part of the Java language itself ? assylias These are custom annotations that are not part of the standard JDK. To be able to use them in your code, you need to add a dependency. At jcip.net , there is a link to the library and its source in the bottom part of the page " Concurrency annotations: jar,

How many threads are spawned in parallelStream in Java 8?

半腔热情 提交于 2019-11-28 05:12:10
In JDK8, how many threads are spawned when i'm using parallelStream? For instance, in the code: list.parallelStream().forEach(/** Do Something */); If this list has 100000 items, how many threads will be spawned? Also, do each of the threads get the same number of items to work on or is it randomly allotted? Umberto Raimondi The Oracle's implementation[1] of parallel stream uses the current thread and in addition to that, if needed, also the threads that compose the default fork join pool ForkJoinPool.commonPool() , which has a default size equal to one less than the number of cores of your

ExecutorService.invokeAll does NOT support collection of runnable task

浪子不回头ぞ 提交于 2019-11-27 22:56:36
Wanted to run collection of Runnable task through invokeAll(..) method of ExecutorService . But that's not supported as of now ( supports collection of Callable task only ) Any specific reason for this? What's the alternative to do something similar. Simply transform the runnables into callables: List<Callable<Void>> callables = new ArrayList<>(); for (Runnable r : runnables) { callables.add(toCallable(r)); } executor.invokeAll(callables); private Callable<Void> toCallable(final Runnable runnable) { return new Callable<Void>() { @Override public Void call() { runnable.run(); return null; } };