Questions tagged [java.util.concurrent]

Java package which contains utility classes commonly useful in concurrent programming. This package includes a few small standardized extensible frameworks, as well as some classes that provide useful functionality and are otherwise tedious or difficult to implement.

java.util.concurrent is a core Java package which contains utility classes commonly useful in concurrent programming. This package includes a few small standardized extensible frameworks, as well as some classes that provide useful functionality and are otherwise tedious or difficult to implement.

Main components include:

  • Executors
  • Queues
  • Timing
  • Synchronizers, Semaphores, CountDownLatch
  • Concurrent Collections

More information is available at the JavaDoc description

1396 questions
-1
votes
1 answer

No deadlock while incrementing synchronized Integer objects

I am tried to implement deadlock in my program and all was ok except one issue which I can't explain. public class Test { public static void main(String[] args) throws InterruptedException { Integer balanceA = 10000; Integer…
-1
votes
1 answer

ConcurrentModificationException thrown when running in debugger

I have the following code that is throwing a ConcurrentModificationException for some reason. It's thrown when I run it in a debugger with a breakpoint set in one of threads, but apparently not otherwise. I've confirmed that the class in question is…
awgtek
  • 1,483
  • 16
  • 28
-1
votes
2 answers

why there's a for(;;) in AbstractQueuedSynchronize enq(final Node node) method

when I read the AbstractQueuedSynchronize source code, about the method private Node enq(final Node node) { for (;;) { Node t = tail; if (t == null) { // Must initialize if (compareAndSetHead(new…
idolice
  • 3
  • 2
-1
votes
1 answer

Does threads of ThreadPoolExecutor not runs concurrently using with PriorityBlockingQueue

I am using java ThreadPoolExecutor to run concurrent thread execution. I used ArrayBlockingQueue to keep threads in queue. But now requirement has changed and I need to add thread run time(no size limit) and it should be prioritized. So i decided…
-1
votes
1 answer

Is non-blocking algorithm also apply for insertion and deletion OR only for some condition like no available space/ no elements to consume

Some blogs mentioned that non-blocking algorithm Data Structures will not block calling threads if no space/element to add/consume. They immediately return with Exception or null. But nowhere I have not found How insertion/deletion works in…
-1
votes
1 answer

When does consumer implemented on blocking queue stop listening to messages

Hi have written a simple producer consumer problem using LinkedblockingQueue. My producer writes 1 to 10 in the queue and consumer reads from the queue using while loop inside run method as Below while(true){ try { …
-1
votes
1 answer

How do i reference a method from a different threads java

I'm trying to reference thread a from thread b, I essentially want to use the getN() method in B class/thread, any help is appreciated //// class help { ///// main { Thread a = new Thread(new A()); Thread b = new Thread(new…
MF DOOM
  • 413
  • 4
  • 19
-1
votes
1 answer

Why is it important to make fields private when working with concurrency?

I'm reading Thinking in JAVA (Ed4, by Bruce Eckel), which says: Note that it’s especially important to make fields private when working with concurrency; otherwise the synchronized keyword cannot prevent another task from accessing a field…
xin
  • 309
  • 2
  • 19
-1
votes
1 answer

Using Java 8 feature takes more time

Sample Program provided to count number of elements from an array that are less than the specified value. Processing time taken by the program varies, using Java 8 forEach and stream takes more time for execution. Please explain if I should go with…
-1
votes
2 answers

unlocking a lock.tryLock with timeout

I have this code Lock lock = new ReentrantLock(); void someMethod() { try { if (lock.tryLock(120, TimeUnit.SECONDS)) { // do stuff } else { // timed out throw new…
mzzzzb
  • 1,422
  • 19
  • 38
-1
votes
2 answers

Multiple threads arrive, the last should do the processing

I'm implementing a logging where multiple threads can write into one List of log. The last thread should write the contents of the List to a file. So the thread which is the last to write into the List should flush the List into a file. What is the…
Roland
  • 7,525
  • 13
  • 61
  • 124
-1
votes
1 answer

Is there a thread safe alternative to using iterator to make changes to a list/set?

Here is my code: class Processor implements Runnable { private int id; private Integer interaction; private Set subset; public volatile static AtomicBoolean notRemoved = new AtomicBoolean(true); public Object dcp; …
-1
votes
3 answers

Convert double check locking from using synchronized to locks in JAVA

Consider the following code implementing double check locking using the synchronized keyword in JAVA 8: private static void redoHeavyInitialisation() { if (needToReinitialise()) { synchronized (MyClass.class) { if…
-1
votes
1 answer

ForkJoinPool BufferedImage Processing Style

I am trying to process an image using a ForkJoinPool in java. I used streams to do some custom operations on the image. I am trying to use ForkJoinPool for getRGB and setRGB methods. How do I achieve parallelism on getRGB methods? @Override …
takirala
  • 1,891
  • 2
  • 16
  • 36
-1
votes
1 answer

Java Inter Thread Communication

I want to create a counter in a new Thread that has a method to get the value of the counter while the thread is running. How can I do it in an easy way?