java.util.concurrent

CompletableFuture is not getting executed. If I use the ExecutorService pool its work as expected but not with the default forkJoin common pool

拜拜、爱过 提交于 2019-12-18 05:06:16
问题 I am trying to run the following class its getting terminated without executing the CompletableFuture. public class ThenApplyExample { public static void main(String[] args) throws Exception { //ExecutorService es = Executors.newCachedThreadPool(); CompletableFuture<Student> studentCompletableFuture = CompletableFuture.supplyAsync(() -> { try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } return 3; })// If I put executorservice created n commented above

ConcurrentHashMap jdk 8 Uses TreeNodes instead of List .. Why? [closed]

半城伤御伤魂 提交于 2019-12-18 04:27:08
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Hi i know the workings of ConcurrentHashMap before JDK 8. I also understood the code: it was pretty modular and not very hard to understand. The code of ConcurrentHashMap in JDK 8 has changed a lot from its previous implementations. Because this question was classified as too

How to give name to a callable Thread? [duplicate]

你说的曾经没有我的故事 提交于 2019-12-17 22:26:34
问题 This question already has answers here : Naming threads and thread-pools of ExecutorService (17 answers) Closed 3 years ago . I am executing a Callable Object using ExecutorService thread pool. I want to give a name to this thread. To be more specific, in older version I did this - Thread thread = new Thread(runnable Task); thread.setName("My Thread Name"); I use thread name in log4j logging, this helps a lot while troubleshooting. Now I am migrating my code from Java 1.4 to Java 1.6. I have

How many threads are spawned in parallelStream in Java 8?

我只是一个虾纸丫 提交于 2019-12-17 15:35:29
问题 In JDK8, how many threads are spawned when i'm using parallelStream? For instance, in the code: list.parallelStream().forEach(/** Do Something */); If this list has 100000 items, how many threads will be spawned? Also, do each of the threads get the same number of items to work on or is it randomly allotted? 回答1: The Oracle's implementation[1] of parallel stream uses the current thread and in addition to that, if needed, also the threads that compose the default fork join pool ForkJoinPool

ExecutorCompletionService? Why do need one if we have invokeAll?

别说谁变了你拦得住时间么 提交于 2019-12-17 15:22:40
问题 If we use an ExecutorCompletionService we can submit a series of tasks as Callable s and get the result interacting with the CompletionService as a queue . But there is also the invokeAll of ExecutorService that accepts a Collection of tasks and we get a list of Future to retrieve the results. As far as I can tell, there is no benefit in using one or over the other (except that we avoid a for loop using an invokeAll that we would have to submit the tasks to the CompletionService ) and

ExecutorCompletionService? Why do need one if we have invokeAll?

对着背影说爱祢 提交于 2019-12-17 15:19:54
问题 If we use an ExecutorCompletionService we can submit a series of tasks as Callable s and get the result interacting with the CompletionService as a queue . But there is also the invokeAll of ExecutorService that accepts a Collection of tasks and we get a list of Future to retrieve the results. As far as I can tell, there is no benefit in using one or over the other (except that we avoid a for loop using an invokeAll that we would have to submit the tasks to the CompletionService ) and

FixedThreadPool vs CachedThreadPool: the lesser of two evils

笑着哭i 提交于 2019-12-17 06:23:07
问题 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

Get the array from an AtomicLongArray

前提是你 提交于 2019-12-14 03:48:28
问题 Using Java 1.6 and the AtomicLongArray, I'd like to "copy" the original AtomicLongArray into a new one. There is a constructor that takes an array (AtomicLongArray(long[])), so I thought I could just get the array from the original one and give it to the constructor. Sadly, the actual long[] in the AtomicLongArray is private and there seem to be no getters for it. Is there any way to do this, meaning copy the values from one AtomicLongArray to another? I can't create my own class based on

Can synchronized blocks be faster than Atomics?

三世轮回 提交于 2019-12-14 03:39:43
问题 Suppose two following counter implementations: class Counter { private final AtomicInteger atomic = new AtomicInteger(0); private int i = 0; public void incrementAtomic() { atomic.incrementAndGet(); } public synchronized void increment() { i++; } } At first glance atomics should be faster and more scalable. And they are, I believe. But are they faster than synchronized blocks all the time? Or some situations exists when this rule is broken (e.g. SMP/Single CPU machine, different CPU ISA, OS

java.util.ConcurrentModificationException android after remove elements from array list

戏子无情 提交于 2019-12-14 03:39:34
问题 I have the folloing code in my android app: /** * callback executed after fetching the data. */ public void OnPointsFetch(ArrayList<Shop> result) { toggleLoader(false); this.shops = result; if(activeFilter == Constants.POINTS_FILTER_AVAILABLE){ for(Shop s : result){ if(s.getClientPoints().getPointsAvailable() == 0){ this.shops.remove(s); } } } else{ for(Shop s : result){ if(s.getClientPoints().getPointsSpent() == 0){ this.shops.remove(s); } } } ptsListAdapter.setCollection(this.shops);