在Java程序中使用Executors而不仅仅是Threads有什么好处.
如
ExecutorService pool = Executors.newFixedThreadPool(2);
void someMethod() {
//Thread
new Thread(new SomeRunnable()).start();
//vs
//Executor
pool.execute(new SomeRunnable());
}
Run Code Online (Sandbox Code Playgroud)
执行程序是否仅限制允许一次运行的线程数(线程池)?它是否实际上将runnable复用到它创建的线程上?如果不是,它只是一种避免每次都必须编写新的Thread(runnable).start()的方法吗?
我有一个正在使用的客户库和传入DataRequest对象拥有userid,timeout并且在它的一些其他领域.现在我使用这个DataRequest对象创建一个URL,然后我使用了一个HTTP调用RestTemplate,我的服务返回一个JSON响应,我用它来创建一个DataResponse对象并将这个DataResponse对象返回给它们.
以下是DataClient客户通过将DataRequest对象传递给我的类.DataRequest如果在getSyncData方法中花费太多时间,我正在使用客户传递的超时值来超时请求.
public class DataClient implements Client {
private RestTemplate restTemplate = new RestTemplate();
// first executor
private ExecutorService service = Executors.newFixedThreadPool(15);
@Override
public DataResponse getSyncData(DataRequest key) {
DataResponse response = null;
Future<DataResponse> responseFuture = null;
try {
responseFuture = getAsyncData(key);
response = responseFuture.get(key.getTimeout(), key.getTimeoutUnit());
} catch (TimeoutException ex) {
response = new DataResponse(DataErrorEnum.CLIENT_TIMEOUT, DataStatusEnum.ERROR);
responseFuture.cancel(true);
// logging …Run Code Online (Sandbox Code Playgroud) 我需要提交一些任务,然后等待所有结果,直到所有结果都可用.它们中的每一个都添加了Stringa Vector(默认情况下是同步的).然后我需要为Vector中的每个结果启动一个新任务,但是只有当所有先前的任务都停止了它们的工作时我才需要这样做.
我想使用Java Executor,特别是我尝试使用Executors.newFixedThreadPool(100)以便使用固定数量的线程(我有一个可变数量的任务,可以是10或500)但我是执行者的新手,我不知道如何等待任务终止.这就像我的程序需要做的伪代码:
ExecutorService e = Executors.newFixedThreadPool(100);
while(true){
/*do something*/
for(...){
<start task>
}
<wait for all task termination>
for each String in result{
<start task>
}
<wait for all task termination>
}
Run Code Online (Sandbox Code Playgroud)
我不能做e.shutdown,因为我有一段时间(真的),我需要重用executorService...
你能帮助我吗?你能给我一个关于java执行器的指南/书吗?
哪个Java同步构造可能为具有固定数量线程的并发迭代处理场景提供最佳性能,如下所述?在我自己试验了一段时间后(使用ExecutorService和CyclicBarrier)并对结果感到有些惊讶,我会感谢一些专家建议和一些新想法.这里的现有问题似乎并不主要关注绩效,因此这个新问题.提前致谢!
该应用程序的核心是一个简单的迭代数据处理算法,与Mac Pro上8个内核的计算负载并行,运行OS X 10.6和Java 1.6.0_07.要处理的数据被分成8个块,每个块被送到Runnable,由固定数量的线程之一执行.并行化算法是相当简单的,它在功能上按预期工作,但它的性能还不是我认为的可能.该应用程序似乎花了很多时间在系统调用同步,所以经过一些分析后,我想知道我是否选择了最合适的同步机制.
该算法的一个关键要求是它需要分阶段进行,因此线程需要在每个阶段结束时进行同步.主线程准备工作(非常低的开销),将它传递给线程,让它们处理它,然后在所有线程完成后继续,重新安排工作(再次非常低的开销)并重复循环.机器专用于此任务,通过使用预分配项的每线程池来最小化垃圾收集,并且可以修复线程数(没有传入请求等,每个CPU核心只有一个线程).
我的第一个实现使用了一个带有8个工作线程的ExecutorService.该程序创建8个任务来完成工作,然后让他们处理它,大致如下:
// create one thread per CPU
executorService = Executors.newFixedThreadPool( 8 );
...
// now process data in cycles
while( ...) {
// package data into 8 work items
...
// create one Callable task per work item
...
// submit the Callables to the worker threads
executorService.invokeAll( taskList );
}
Run Code Online (Sandbox Code Playgroud)
这在功能上运行良好(它做它应该做的事情),对于非常大的工作项,确实所有8个CPU都变得高负载,就像预期允许的处理算法一样(一些工作项将比其他工作项完成得更快,然后空闲) .但是,随着工作项变小(并且实际上不受程序控制),用户CPU负载急剧缩小:
blocksize | system | user | cycles/sec
256k 1.8% 85% 1.30
64k 2.5% 77% 5.6
16k 4% …Run Code Online (Sandbox Code Playgroud) java performance multithreading executorservice cyclicbarrier
我可以在没有ExecutorService的情况下使用Callable线程吗?我们可以使用Runnable的实例和没有ExecutorService的Thread的子类,这个代码可以正常工作.但是这段代码始终如一:
public class Application2 {
public static class WordLengthCallable implements Callable {
public static int count = 0;
private final int numberOfThread = count++;
public Integer call() throws InterruptedException {
int sum = 0;
for (int i = 0; i < 100000; i++) {
sum += i;
}
System.out.println(numberOfThread);
return numberOfThread;
}
}
public static void main(String[] args) throws InterruptedException {
WordLengthCallable wordLengthCallable1 = new WordLengthCallable();
WordLengthCallable wordLengthCallable2 = new WordLengthCallable();
WordLengthCallable wordLengthCallable3 = new WordLengthCallable();
WordLengthCallable wordLengthCallable4 = new …Run Code Online (Sandbox Code Playgroud) java concurrency multithreading executorservice java.util.concurrent
使用中的低级差异是什么:
ForkJoinPool = new ForkJoinPool(X);
Run Code Online (Sandbox Code Playgroud)
和
ExecutorService ex = Executors.neWorkStealingPool(X);
Run Code Online (Sandbox Code Playgroud)
其中X是所需的并行级别,即线程运行..
根据文档我发现它们相似.还告诉我哪一个在任何正常用途下更合适和安全.我有1.3亿个条目写入BufferedWriter并使用Unix排序按第1列排序.
另请告诉我如果可能的话要保留多少个线程.
注意:我的系统有 8个核心处理器和 32 GB RAM.
multithreading executorservice fork-join executors forkjoinpool
我正在Executors.newScheduledThreadPool()创建一个ScheduledExecutorService,指定线程数如下:
int corePoolSize = 42;
ScheduledExecutorService foo = Executors.newScheduledThreadPool(corePoolSize);
Run Code Online (Sandbox Code Playgroud)
根据JavaDocs,corePoolSize参数设置
即使它们处于空闲状态,也要保留在池中的线程数.
这是否意味着此ExecutorService实现可能会corePoolSize根据需要创建多个线程,类似于缓存的线程池?
想通过ExecutorService的 invokeAll(..)方法运行Runnable任务的集合.但是现在不支持(仅支持可调用任务的集合)
有什么具体的原因吗?做类似事情的替代方法是什么.
CompletableFuture::supplyAsync(() -> IO bound queries)
如何为CompletableFuture :: supplyAsync选择Executor以避免污染ForkJoinPool.commonPool().
有许多选项Executors(newCachedThreadPool,newWorkStealingPool,newFixedThreadPool等)
我在这里阅读了关于新ForkJoinPool的内容
如何为我的用例选择合适的?
java executorservice java-8 threadpoolexecutor completable-future
我有一个场景,我必须为同一个callable异步执行5个线程.据我了解,有两种选择:
1)使用submit(Callable)
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<String>> futures = new ArrayList<>();
for(Callable callableItem: myCallableList){
futures.add(executorService.submit(callableItem));
}
Run Code Online (Sandbox Code Playgroud)
2)使用invokeAll(Callable集合)
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<String>> futures = executorService.invokeAll(myCallableList));
Run Code Online (Sandbox Code Playgroud)
executorservice ×10
java ×9
concurrency ×3
callable ×1
executors ×1
fork-join ×1
forkjoinpool ×1
java-8 ×1
performance ×1
threadpool ×1