java.util.concurrent

Why CompletableFuture.runAsync is not executed?

落爺英雄遲暮 提交于 2020-12-27 05:32:48
问题 I consider that main thread must end after sub thread.But below code shows the process finished before print the "async end".What is the reason?Can anybody explain?Thx. import java.util.concurrent.CompletableFuture; public class Test { public static void main(String[] args) { CompletableFuture.runAsync(() -> { try { System.out.println("async start"); Thread.sleep(3000); System.out.println("async end"); } catch (InterruptedException e) { e.printStackTrace(); } }); System.out.println("main end"

Why lambda inside map is not running?

*爱你&永不变心* 提交于 2020-11-28 08:57:47
问题 I am trying to learn concurrency and lambdas in java 8. But my code is not entering lambda block inside map. List<Book> bookList = new ArrayList<Book>(); isbnList .stream() .map(isbn -> (CompletableFuture.supplyAsync( () -> { try { List<String> pageContents = getUrlContents(webLink + isbn); return new Book( parseBookTitle(pageContents), isbn, parseRank(pageContents) ); } catch (IOException ex) { return null; } })).thenApply(a -> bookList.add(a)) ); While debugging, code is exiting at .map

Why lambda inside map is not running?

≡放荡痞女 提交于 2020-11-28 08:57:25
问题 I am trying to learn concurrency and lambdas in java 8. But my code is not entering lambda block inside map. List<Book> bookList = new ArrayList<Book>(); isbnList .stream() .map(isbn -> (CompletableFuture.supplyAsync( () -> { try { List<String> pageContents = getUrlContents(webLink + isbn); return new Book( parseBookTitle(pageContents), isbn, parseRank(pageContents) ); } catch (IOException ex) { return null; } })).thenApply(a -> bookList.add(a)) ); While debugging, code is exiting at .map

Spring - Have TaskExecutor and TaskScheduler backed by the same thread pool

我是研究僧i 提交于 2020-07-30 03:29:06
问题 I am a bean for TaskScheduler and TaskExecutor as following: @Bean public TaskScheduler taskScheduler() { ThreadPoolTaskScheduler s = new ThreadPoolTaskScheduler(); s.setThreadNamePrefix("Task-Scheduler-"); s.setPoolSize(10); s.setRemoveOnCancelPolicy(true); return s; } @Bean public TaskExecutor taskExecutor() { ThreadPoolTaskExecutor e = new ThreadPoolTaskExecutor(); e.setThreadNamePrefix("Task-Executor-"); e.setMaxPoolSize(10); e.setCorePoolSize(10); return e; } Is it possible to share the

CompletableFuture, mutable objects and memory visibility

青春壹個敷衍的年華 提交于 2020-07-04 07:36:10
问题 I'm trying to understand how CompletableFuture in Java 8 interacts with the Java memory model. It seems to me that for programmer sanity, the following should ideally hold true: Actions in the thread that completes a CompletableFuture happen-before any completions dependent stages are executed Actions in the thread that registers a completion creates a dependent stage happen-before the completion dependent stage is executed There's a note in java.util.concurrent documentation saying that:

Why should we call join after invokeAll method?

家住魔仙堡 提交于 2020-06-26 07:05:20
问题 I am trying to learn about the ForkJoinPool framework and came across the below example: public class ArrayCounter extends RecursiveTask<Integer> { int[] array; int threshold = 100_000; int start; int end; public ArrayCounter(int[] array, int start, int end) { this.array = array; this.start = start; this.end = end; } protected Integer compute() { if (end - start < threshold) { return computeDirectly(); } else { int middle = (end + start) / 2; ArrayCounter subTask1 = new ArrayCounter(array,

ExecutorService JVM doesn't terminate [duplicate]

五迷三道 提交于 2020-06-26 04:45:12
问题 This question already has answers here : Java ServiceExecutor terminating condition (4 answers) Closed 5 years ago . I don't understand why I have to call executorService.shutdown() explicitly to terminate executorService. If I will not call shutdown() then the JVM will not terminate on its own. What is wrong with my program or what concept I am lacking? public class ExecutorServiceExample { public static class Task1 implements Runnable { @Override public void run() { try { Thread.sleep(1000)

Handle back-pressure in FixedThreadPool

戏子无情 提交于 2020-06-01 05:12:26
问题 How to deal with back-pressure in Java using thread pool? How to reject new tasks so there are no more than N submitted tasks. N - is the maximum number of allowed tasks in submission queue, which include new, running, paused (not finished) tasks. Use case Users submit calculation tasks that run for some time . Sometimes, there are so many users submitting tasks at the same time. How to reject new tasks if there are already N tasks submitted. In other words, the total number of submitted (not

Synchronization in java ForkJoinPool compute() method

♀尐吖头ヾ 提交于 2020-06-01 05:07:56
问题 in Java: The Complete Reference we read: In general, a ForkJoinTask should not use synchronized methods or synchronized blocks of code. Also, you will not normally want to have the compute( ) method use other types of synchronization, such as a semaphore Why should I avoid synchronizing in compute()? Is it still possible in some situation to use synchronization such as semaphore or synchronized? What other method should I use from java.util.concurrent to have scallable number of threads as in

Synchronization in java ForkJoinPool compute() method

南笙酒味 提交于 2020-06-01 05:07:07
问题 in Java: The Complete Reference we read: In general, a ForkJoinTask should not use synchronized methods or synchronized blocks of code. Also, you will not normally want to have the compute( ) method use other types of synchronization, such as a semaphore Why should I avoid synchronizing in compute()? Is it still possible in some situation to use synchronization such as semaphore or synchronized? What other method should I use from java.util.concurrent to have scallable number of threads as in