java.util.concurrent

Whether to use invokeAll or submit - java Executor service

孤街醉人 提交于 2019-11-27 01:53:20
问题 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

Is there java.util.concurrent equivalent for WeakHashMap?

帅比萌擦擦* 提交于 2019-11-27 01:19:05
问题 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 回答1: Guava's CacheBuilder class allows you to do this easily. CacheBuilder.newBuilder().weakKeys().build()

@GuardedBy , @ThreadSafe ,@NotThreadSafe

旧街凉风 提交于 2019-11-27 00:52:25
问题 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 ? 回答1: 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

FixedThreadPool vs CachedThreadPool: the lesser of two evils

柔情痞子 提交于 2019-11-26 23:32:40
I have a program that spawns threads (~5-150) which perform a bunch of tasks. Originally, I used a FixedThreadPool because this similar question suggested they were better suited for longer lived tasks and with my very limited knowledge of multithreading, I considered the average life of the threads (several minutes) " long lived ". However, I recently added the capability to spawn additional threads and doing so takes me above the thread limit I set. In this case, would it be better to guess and increase the number threads I can allow or to switch to a CachedThreadPool so I have no wasted

ExecutorService.invokeAll does NOT support collection of runnable task

喜夏-厌秋 提交于 2019-11-26 23:20:16
问题 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. 回答1: 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

Is ConcurrentHashMap totally safe?

时光怂恿深爱的人放手 提交于 2019-11-26 19:46:28
this is a passage from JavaDoc regarding ConcurrentHashMap . It says retrieval operations generally do not block, so may overlap with update operations. Does this mean the get() method is not thread safe? "However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access. This class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details. Retrieval operations (including get) generally do not block, so may

Thread safe Hash Map?

社会主义新天地 提交于 2019-11-26 18:59:54
问题 I am writing an application which will return a HashMap to user. User will get reference to this MAP. On the backend, I will be running some threads which will update the Map. What I have done so far? I have made all the backend threads so share a common channel to update the MAP. So at backend I am sure that concurrent write operation will not be an issue. Issues I am having If user tries to update the MAP and simultaneously MAP is being updated at backend --> Concurrent write operation

Is there a Mutex in Java?

不想你离开。 提交于 2019-11-26 18:15:35
Is there a Mutex object in java or a way to create one? I am asking because a Semaphore object initialized with 1 permit does not help me. Think of this case: try { semaphore.acquire(); //do stuff semaphore.release(); } catch (Exception e) { semaphore.release(); } if an exception happens at the first acquire, the release in the catch block will increase the permits, and the semaphore is no longer a binary semaphore. Will the correct way be? try { semaphore.acquire(); //do stuff } catch (Exception e) { //exception stuff } finally { semaphore.release(); } Will the above code ensure that the

scala.concurrent.blocking - what does it actually do?

我的梦境 提交于 2019-11-26 17:57:24
问题 I have spent a while learning the topic of Scala execution contexts, underlying threading models and concurrency. Can you explain in what ways does scala.concurrent.blocking "adjust the runtime behavior" and "may improve performance or avoid deadlocks" as described in the scaladoc? In the documentation, it is presented as a means to await api that doesn't implement Awaitable. (Perhaps also just long running computation should be wrapped?). What is it that it actually does? Following through

What is adaptive spinning w.r.t lock acquisition?

匆匆过客 提交于 2019-11-26 17:48:25
问题 One of the four major changes brought to java 6 with regards to improving the performance of intrinsic locks is Adapative Spinning technique. What exactly is adaptive spinning? Is it a combination of spinlocks and mutex lock? Can someone explain in an easy to understand way the usefulness of this strategy which by default is available from JDK6 onwards. 回答1: What exactly is adaptive spinning? To quote from this Java 6 performance page: Adaptive spinning is an optimization technique where a