由于使用ExecutorService可submit一个Callable任务并返回Future,为什么需要使用FutureTask包装Callable的任务和使用的方法execute?我觉得他们都做同样的事情.
我正在开发一个多线程项目,我需要生成多个线程来测量我的客户端代码的端到端性能,因为我正在进行负载和性能测试.所以我创建了以下使用的代码ExecutorService.
以下是代码ExecutorService:
public class MultithreadingExample {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(20);
for (int i = 0; i < 100; i++) {
executor.submit(new NewTask());
}
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
}
}
class NewTask implements Runnable {
@Override
public void run() {
//Measure the end to end latency of my client code
}
}
Run Code Online (Sandbox Code Playgroud)
问题陈述:
现在我正在阅读互联网上的一些文章.我发现也有ThreadPoolExecutor.所以我很困惑我应该使用哪一个.
如果我将以上代码替换为:
ExecutorService executor = Executors.newFixedThreadPool(20);
for (int i = 0; i < 100; i++) …Run Code Online (Sandbox Code Playgroud) java multithreading executorservice blockingqueue threadpoolexecutor
使用ExecutorService过度运行的线程Runnable进入Thread构造函数有什么好处?
每当我打电话shutdownNow()或shutdown()不关机时.我读到了一些线程,它说不能保证关闭 - 有人能为我提供一个好方法吗?
我有一个方法,通过超时执行一些任务.我使用ExecutorServer.submit()来获取Future对象,然后使用超时调用future.get().这工作正常,但我的问题是处理我的任务可以抛出的已检查异常的最佳方法.以下代码有效,并保留已检查的异常,但如果方法签名中的已检查异常列表发生更改,则它似乎非常笨拙且容易中断.
对于如何解决这个问题,有任何的建议吗?我需要针对Java 5,但我也很想知道在较新版本的Java中是否有好的解决方案.
public static byte[] doSomethingWithTimeout( int timeout ) throws ProcessExecutionException, InterruptedException, IOException, TimeoutException {
Callable<byte[]> callable = new Callable<byte[]>() {
public byte[] call() throws IOException, InterruptedException, ProcessExecutionException {
//Do some work that could throw one of these exceptions
return null;
}
};
try {
ExecutorService service = Executors.newSingleThreadExecutor();
try {
Future<byte[]> future = service.submit( callable );
return future.get( timeout, TimeUnit.MILLISECONDS );
} finally {
service.shutdown();
}
} catch( Throwable t ) { //Exception handling of nested exceptions is …Run Code Online (Sandbox Code Playgroud) 有什么区别
ExecutorService eService = Executors.newFixedThreadPool(2);
eService.execute(new TestThread6());
eService.execute(new TestThread6());
eService.execute(new TestThread6());
eService.awaitTermination(1, TimeUnit.NANOSECONDS);
eService.shutdown();
Run Code Online (Sandbox Code Playgroud)
和
eService.shutdown();
eService.awaitTermination(1, TimeUnit.NANOSECONDS);
Run Code Online (Sandbox Code Playgroud)
我真的不明白shutdown().此方法不会等待先前提交的任务完成执行.这是否意味着shutdown()可以终止已提交但未完成的任务?我尝试了一些例子,他们没有证明,请举个例子.
如果我们使用ExecutorCompletionService,我们可以提交一系列任务作为Callables,并将结果与CompletionServiceas进行交互queue.
但也有在invokeAll的ExecutorService,它接受一个Collection任务,我们得到的名单Future,以检索结果.
据我所知,有在使用一个或比其他任何好处(除了我们避免for使用循环invokeAll,我们将不得不submit对任务的CompletionService),基本上他们是有轻微的差别同样的想法.
那么为什么有两种不同的方式来提交一系列任务呢?我是否正确表现他们是相同的?是否有一个比另一个更合适的情况?我想不出一个.
java concurrency multithreading executorservice java.util.concurrent
我正在使用ExecutorService线程池执行可调用对象.我想给这个帖子命名.
更具体地说,在旧版本中,我这样做了 -
Thread thread = new Thread(runnable Task);
thread.setName("My Thread Name");
Run Code Online (Sandbox Code Playgroud)
我在log4j日志记录中使用线程名称,这在故障排除时有很大帮助.现在我将我的代码从Java 1.4迁移到Java 1.6.我写了这个(在下面给出) - 但我不知道如何给这个帖子命名.
private final ExecutorService executorPool = Executors.newCachedThreadPool();
Future<String> result = executorPool.submit(callable Task);
Run Code Online (Sandbox Code Playgroud)
请给我一些想法给这个帖子命名?
为什么,为什么不java.util.concurrent提供其ExecutorServices 的队列长度指标?最近我发现自己做了这样的事情:
ExecutorService queue = Executors.newSingleThreadExecutor();
AtomicInteger queueLength = new AtomicInteger();
...
public void addTaskToQueue(Runnable runnable) {
if (queueLength.get() < MAX_QUEUE_LENGTH) {
queueLength.incrementAndGet(); // Increment queue when submitting task.
queue.submit(new Runnable() {
public void run() {
runnable.run();
queueLength.decrementAndGet(); // Decrement queue when task done.
}
});
} else {
// Trigger error: too long queue
}
}
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但......我认为这应该作为一部分实现ExecutorService.这是一个愚蠢的错误,容易携带一个与实际队列分开的计数器,计数器应该指示它的长度(让我想起C数组).但是,ExecutorServices是通过静态工厂方法获得的,因此无法简单地扩展优秀的单线程执行器并添加队列计数器.所以我该怎么做:
我有一个使用Executor框架的Java应用程序,我有这样的代码
protected ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5)
我的理解是,JVM内部将创建一个包含5个线程的池.现在,当我在一个分析器中检查执行时,我会得到类似的东西thread-pool2,thread-pool3等等.
Some of these thread pools are created by the server and some are created by me,我需要一种方法来区分由我创建的和由服务器创建的.
我想如果我可以命名线程池它应该做的伎俩,但是没有看到任何API允许我这样做.
提前致谢.
executorservice ×10
java ×10
concurrency ×3
callable ×1
executor ×1
future ×1
futuretask ×1
queue ×1
shutdown ×1
threadpool ×1