FAQ Database Discussion Community


Executing one thread at a time sequentially

java,multithreading,concurrency,executorservice,java.util.concurrent
I have several threads being created in a for loop like this: for (int i = 0; i<5; i++) { new Thread() { //do stuff } } but I need to make sure that these threads execute one after the other rather than all at the same time. what is...

Synchronized HashMap vs ConcurrentHashMap write test

java,java.util.concurrent
I learn java.util.concurrency and I have found one article about performance (http://www.javamex.com/tutorials/concurrenthashmap_scalability.shtml). And I decided to repeat one little part of those performance test for studying purpose. I have written the write test for HashMap and ConcurrentHashMap. I have two questions about it: Is it true, that for the best...

How to implement asynchronous queue?

java,concurrency,java.util.concurrent
Given following variation of queue: interface AsyncQueue<T> { //add new element to the queue void add(T elem); //request single element from the queue via callback //callback will be called once for single polled element when it is available //so, to request multiple elements, poll() must be called multiple times with...

Java concurrency, stream behavior

java,concurrency,java-8,java.util.concurrent
Methods getFirst() and getSecond() of this class is invoked concurrently. It is a part of a web app. Internal maps populated as well with no concurrency. public class MyClass { private Map<String, List<List<String>>> first; private Map<String, List<List<String>>> second; public MyClass() { first = new ConcurrentHashMap<>(); second = new ConcurrentHashMap<>(); }...

Confused by docs and source of CountedCompleter

java,concurrency,java.util.concurrent,forkjoinpool
Here is a code fragment of java.util.concurrent.CountedCompleter class (JDK 1.8.0_25). /** * If the pending count is nonzero, decrements the count; * otherwise invokes {@link #onCompletion(CountedCompleter)} * and then similarly tries to complete this task's completer, * if one exists, else marks this task as complete. */ public final void...

Why does Java concurrent processing not work with newly instantiated objects, while it works with deserialized objects of the same class?

java,multithreading,serialization,concurrency,java.util.concurrent
I am using java.util.concurrent.ExecutorService to perform a concurrent calculation on all available processing resources. In the code below an instance of MyProcessor class in its performParallelProcessing method creates a number of ProcessingExecutor class instances and submits them to the ExecutorService instance expecting to get the corresponding callbacks. The processing happens...

Behavior of entrySet().removeIf in ConcurrentHashMap

java,multithreading,java.util.concurrent,concurrenthashmap,concurrentmodification
I would like to use ConcurrentHashMap to let one thread delete some items from the map periodically and other threads to put and get items from the map at the same time. I'm using map.entrySet().removeIf(lambda) in the removing thread. I'm wondering what assumptions I can make about its behavior. I...

Using java futures without requiring local mutable state

java,multithreading,java.util.concurrent
I've written some code that essentially is responsible for orchestrating a number of API's in sequence through a library method I provide to my clients called "orchestrate" (yes I know so original). What sits behind this orchestrate method is nothing more than a loop that executes API's in the order...

Usage of lazySet on AtomicXXX in Java

java,concurrency,java.util.concurrent
From this question : AtomicInteger lazySet vs. set and form this link : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html I could gather following points lazySet could be faster than set lazySet uses store-store barrier (writes before are honored but not the contended writes, which were yet to happen) I could find one use-case where it...

How to figure out N threads I need in a parallel execution context?

java,multithreading,java.util.concurrent
I'm working in a redelivery system. This system attempt to execute an action, if the action fails, it try to execute again two times with an interval of five minutes, so I use the ExecutorService implementation to perform the first execution and ScheduledExecutorService to schedule the other ones, depending of...

What is the best approach for loading data from DB by multiple threads

java,multithreading,jdbc,spring-batch,java.util.concurrent
I have some data in database in parent child relation, where my table is actually representing a forest of tree data structure. And the table structure is like: row1 parent: null row2 parent:row1 row3 parent:row2 row4 parent:row1 Now when I am loading this data from DB to my data structure...

ConcurrentModificationException in iterator.next()

java,multithreading,java.util.concurrent
I have next code in background thread private List<IStartAction> mActions = Collections.synchronizedList(new ArrayList<IStartAction>()); protected void removeNonApplicableActions() { Iterator<IStartAction> iterator = mActions.iterator(); while (iterator.hasNext()) { IStartAction action = iterator.next(); if (!action.isApplicable()) { iterator.remove(); } } } When i run this in main thread got ConcurrentModificationException into iterator.next(). Why is this happening?...

Java concurrency issue - Locks and Synchronize method

java,multithreading,java.util.concurrent
I am working on Re entrant locks and trying to correlate it with Synchronize. However both these classes are giving me unexpected results. I am expecting the arrayList to have 0 to 9. but that value never comes in both these programs. Please suggest. With lock: package Threads; import java.util.ArrayList;...

java model client server architecture with lock on resources

java,client-server,java.util.concurrent,concurrent-programming
I have to develop a client server architecture with Java (server side), but I need some advice. Situation: a server that exposes an action decrementValue the server has a variable called Value = integer value some clients can send a decrementValue request to the server server, for each requests, do:...

Future cancel method documentation

java,concurrency,documentation,future,java.util.concurrent
According to http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html isDone returns true if cancel(boolean mayInterruptIfRunning) was called. After this method returns, subsequent calls to isDone() will always return true. However, it is possible that task is running and mayInterruptIfRunning is set to false. So, what should return isDone() right after that call? true because of cancel...

ExecutorService JVM doesn't terminate [duplicate]

java,multithreading,executorservice,java.util.concurrent
This question already has an answer here: Java ServiceExecutor terminating condition 4 answers I don't understand why I have to call executorService.shutdown() explicitly to terminate executorService. If I will not call shutdown() then the JVM will not terminate on its own. What is wrong with my program or what...

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

scala,concurrency,java.util.concurrent
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...

Strange call stack when using Callable

java,java.util.concurrent
I see strange result when dumping a call stack from a call using Executors and callable. The method from the callable appears twice in the call stack. [email protected], prio=5, in group 'main', status: 'RUNNING' at com.test.tracked.ACallable.call(ACallable.java:15) at com.test.tracked.ACallable.call(ACallable.java:9) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at...

accessing data from concurrentHashMap while it gets updated in background android

android,hashmap,java.util.concurrent,concurrenthashmap
I have a static concurrentHashMap object which is been updated in background. While it is getting updated, I want to access the values from it in another thread. I am using concurrentHashMap which I understand from the documentation and thinks that it would suit this scenrio Here is what I...