Filtering CompletableFutures
When working with CompletableFuture
(as opposed to Project Rector’s Mono
), is there some alternative to Project Reactor’s filter()
method? That is, I want the future to be empty if a passed predicate evaluates to false
Is AtomicIntegers thread safe and the output of the code is nondeterministic or deterministic?
import java.util.concurrent.atomic.AtomicInteger; class Counter implements Runnable { private static AtomicInteger i = new AtomicInteger(3); public void run() { System.out.print(i.getAndDecrement()); } } public class Test { public static void main(String[] args) { Thread t1 = new Thread(new Counter()); Thread t2 = new Thread(new Counter()); Thread t3 = new Thread(new Counter()); Thread[] threads = {t1, t2, t3}; […]
Is it thread-safe by default to have nested CopyOnWriteArrayList?
Lets imagine I have the following code:
Java concurrency without using Thread or ExecutorService
There is this task that was given to me to do and I have tried all my best but couldn’t be able to achieve it. The task is to write a Java Program that will produce a pancake and at the same time users will be picked at random to eat the produced pancakes. The two things must be done concurrently, meaning that as the pancake is being produced, users will check if there is any available pancake produced and then eat it. Each user is expected to eat a maximum of 5 pancakes and the producer of the pancake is expected to produce a maximum of 12 pancakes within the given time. The whole of this thing is to happen within only 20 seconds and then the program should be terminated and print out the report after the 20 seconds. The Instruction was also that I should make the two things to run concurrently but not to use Thread or ExecutorService. I really need help some help on how to achieve it. Here is what I tried and I am getting an infinite loop on the producePancake method, it doesn’t even allow the eatPancake method to execute.
Java Concurrency and failed JUnit test
I have this JUnit test:
Why all threads are starving in my Dining-Philosophers solution?
I’m learning Concurrency in java and I’m trying to solve Dining-Philosopher problem with tryLock.