java.util.concurrent

Why there is no ConcurrentLinkedHashMap class in jdk?

陌路散爱 提交于 2019-12-04 21:41:39
问题 This question follows directly from my previous question here in SO . I think the answer to my second question is no . So I would like understand why there is no ConcurrentLinkedHashMap in java.util.concurrent package ? I mean there is a ConcurrentHashMap but no ConcurrentLinkedHashMap . Does it not make any sense at all to have such a class in Concurrent environments ? I mean what is the main technical reason here for its non availabalility ? Is there something similar in Guava/ Apache

Per-key blocking Map in Java

此生再无相见时 提交于 2019-12-04 18:13:01
问题 I'm dealing with some third-party library code that involves creating expensive objects and caching them in a Map . The existing implementation is something like lock.lock() try { Foo result = cache.get(key); if (result == null) { result = createFooExpensively(key); cache.put(key, result); } return result; } finally { lock.unlock(); } Obviously this is not the best design when Foos for different keys can be created independently. My current hack is to use a Map of Futures : lock.lock();

Directory Scanner in Java

为君一笑 提交于 2019-12-04 14:40:59
Scan a set of directories continuously for a set of file name filters. For each file name filter arrived, process the file and repeat the steps for all What can be the recommended design for this in jdk 1.5 , possibly using java.concurrent.Executor and Future I have done a similar task with the web crawler.Just a few changes had to be made... It is a concurrent implementation with newly found directories getting scanned by the thread pool in the Executor Framework.It uses concurrent collections for Queue and List to hold the indexed files. The indexer picks up the files from the queue and does

CompletableFuture — Aggregate Future to Fail Fast

北城余情 提交于 2019-12-04 09:19:59
I have been using the CompletableFuture.allOf(...) helper to create aggregate futures that will only become "done" when their composite futures are marked as complete, i.e: CompletableFuture<?> future1 = new CompletableFuture<>(); CompletableFuture<?> future2 = new CompletableFuture<>(); CompletableFuture<?> future3 = new CompletableFuture<>(); CompletableFuture<?> future = CompletableFuture.allOf(future1, future2, future3); I would like a slight variation on this functionality, where the aggregate future is market as complete when: All futures have completed successfully OR Any one future has

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-04 09:13:32
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, programme work as expected. .thenApply(i -> { for (int j = 0; j <= i; j++) { System.out.println(

How does java.util.concurrent.Executor work?

送分小仙女□ 提交于 2019-12-04 09:08:17
How does java.util.concurrent.Executor create the "real" thread? Suppose I am implementing Executor or using any executor service (like ThreadPoolExecutor). How does JVM internally work? MJB It calls ThreadFactory . Look at the Executors class. Note they all have an overloaded argument where you can supply a ThreadFactory implementation. The ThreadFactory interface is basically public Thread newThread(Runnable runnable); and the default implementation if not supplied basically just is return new Thread(runnable); Why override this - well it's very useful for setting the Thread name and daemon

is there any Concurrent LinkedHashSet in JDK6.0 or other libraries?

百般思念 提交于 2019-12-04 09:04:38
my code throw follow exception: java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761) at java.util.LinkedList$ListItr.next(LinkedList.java:696) at java.util.AbstractCollection.addAll(AbstractCollection.java:305) at java.util.LinkedHashSet.<init>(LinkedHashSet.java:152) ... I want a ConcurrentLinkedHashSet to fix it, but I only found ConcurrentSkipListSet in java.util.concurrent ,this is TreeSet , not LinkedHashSet any easies way to get ConcurrentLinkedHashSet in JDK6.0? thanks for help :) A ConcurrentModificationException has

Can you interrupt BufferedReader.readLine() with Future.cancel(true)?

倖福魔咒の 提交于 2019-12-04 07:22:27
Let's say I started a thread and I have something like this: ...//initiate all the socket connection future = executor.submit ( new Runnable() { public void run() { ... ... while ((str = in.readLine()) != null) { //do something here } } ); executor is a ExecutorService object and in is a BufferedReader object I know you can close the socket from a different thread to interrupt this thread. But when I try to use future.cancel(true) method, even though it returns true, the thread seems still running, anybody know why? or in.readLine() cannot be interrrupted this way? Can you interrupt

Entering in block with an Intrinsic Lock

谁都会走 提交于 2019-12-04 03:54:41
问题 I don't see how the following code produces output that appears to contravene the definition of an object lock. Surely only one thread should be allowed to print the "acquired lock" message yet they both do? class InterruptThreadGroup { public static void main(String[] args) { Object lock = new Object(); MyThread mt1 = new MyThread(lock); MyThread mt2 = new MyThread(lock); mt1.setName("A"); mt1.start(); mt2.setName("B"); mt2.start(); try { Thread.sleep(2000); } catch (InterruptedException e)

ThreadPoolExecutor vs ForkJoinPool: stealing subtasks

 ̄綄美尐妖づ 提交于 2019-12-04 03:50:42
From java docs, A ForkJoinPool differs from other kinds of ExecutorService mainly by virtue of employing work-stealing: all threads in the pool attempt to find and execute subtasks created by other active tasks (eventually blocking waiting for work if none exist). This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTasks). When setting asyncMode to true in constructors, ForkJoinPools may also be appropriate for use with event-style tasks that are never joined. After going through below ForkJoinPool example , Unlike ThreadPoolExecutor, I have not seen