java.util.concurrent

Is there a .thenCompose() for CompletableFuture that also executes exceptionally?

霸气de小男生 提交于 2020-01-03 08:26:10
问题 I want to execute a CompletableFuture once another CompletableFuture finishes, regardless of whether or not the first one completes exceptionally ( .thenCompose() only runs when execution completes normally). For example: CompletableFuture.supplyAsync(() -> 1L) .whenComplete((v, e) -> CompletableFuture.runAsync(() -> { try { Thread.sleep(1000); System.out.println("HERE"); } catch(InterruptedException exc) { return; } })) .whenComplete((v, e) -> System.out.println("ALL DONE")); This prints ALL

Is there a .thenCompose() for CompletableFuture that also executes exceptionally?

廉价感情. 提交于 2020-01-03 08:26:02
问题 I want to execute a CompletableFuture once another CompletableFuture finishes, regardless of whether or not the first one completes exceptionally ( .thenCompose() only runs when execution completes normally). For example: CompletableFuture.supplyAsync(() -> 1L) .whenComplete((v, e) -> CompletableFuture.runAsync(() -> { try { Thread.sleep(1000); System.out.println("HERE"); } catch(InterruptedException exc) { return; } })) .whenComplete((v, e) -> System.out.println("ALL DONE")); This prints ALL

Why java.util.concurrent.atomic.AtomicBoolean is internally implemented with int?

此生再无相见时 提交于 2020-01-01 03:58:08
问题 AtomicBoolean stores its value in: private volatile int value; Then, for example, extracting its value is done like this: public final boolean get() { return value != 0; } What is the reason behind it? Why boolean was not used? 回答1: AFAIK, int is the smallest type CAS operations can be implemented across different machine types. Note: as object allocations are 8 byte aligned, using a smaller type wouldn't save any memory. 回答2: This probably is to be able to base several of the Atomic classes

implement-your-own blocking queue in java

若如初见. 提交于 2019-12-31 09:45:11
问题 I know this question has been asked and answered many times before, but I just couldn't figure out a trick on the examples found around internet, like this or that one. Both of these solutions check for emptiness of the blocking queue's array/queue/linkedlist to notifyAll waiting threads in put() method and vice versa in get() methods. A comment in the second link emphasizes this situation and mentions that that's not necessary. So the question is; It also seems a bit odd to me to check

OpenJDK's LinkedBlockingQueue implementation: Node class and GC [duplicate]

喜欢而已 提交于 2019-12-31 02:56:09
问题 This question already has answers here : Strange code in java.util.concurrent.LinkedBlockingQueue (4 answers) Closed last year . I'm a little confused by the structure of the Node class in OpenJDK's implementation of LinkedBlockingQueue (in java.util.concurrent). I've reproduced the description of the node class below: static class Node<E> { E item; /** * One of: * - the real successor Node * - this Node, meaning the successor is head.next * - null, meaning there is no successor (this is the

ForkJoinPool resets thread interrupted state

微笑、不失礼 提交于 2019-12-31 00:46:10
问题 I just noticed the following phenomena when cancelling a Future returned by ForkJoinPool. Given the following example code: ForkJoinPool pool = new ForkJoinPool(); Future<?> fut = pool.submit(new Callable<Void>() { @Override public Void call() throws Exception { while (true) { if (Thread.currentThread().isInterrupted()) { // <-- never true System.out.println("interrupted"); throw new InterruptedException(); } } } }); Thread.sleep(1000); System.out.println("cancel"); fut.cancel(true); The

What is the reason behind “get” method implementation of AtomicMarkableReference in java?

故事扮演 提交于 2019-12-30 11:17:13
问题 In java an AtomicMarkableReference can be used to update atomically an object reference along with a mark bit. The javadoc states: Implementation note: This implementation maintains markable references by creating internal objects representing "boxed" [reference, boolean] pairs. This is true according to what can be seen in the java 8 source code of the class: package java.util.concurrent.atomic; public class AtomicMarkableReference<V> { private static class Pair<T> { final T reference; final

Need simple explanation how “lock striping” works with ConcurrentHashMap

一曲冷凌霜 提交于 2019-12-29 03:19:05
问题 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

Regarding countdown latch implementation

南笙酒味 提交于 2019-12-25 18:55:11
问题 I have been going through the program which starts three threads and print their corresponding value such that T3 is executed first, then the T1 thread, and lastly the T2 thread is executed. Below is the program. I just want to know if you guys could help in converting this program with respect to countdown latch, as I want to develop it using this mechanism or it can be also done through counting semaphore. From the answer to this related question: public class Test { static class Printer

Multiple threads arrive, the last should do the processing

浪子不回头ぞ 提交于 2019-12-25 18:50:41
问题 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 best way to accomplish this? For the List I just need one of the concurrent classes that is efficient for multiple writers and one reader. 回答1: In my case the simple solution was to implement Closeable and then do the flushing in the close method.