我有一个场景,我必须为同一个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)
我想处理ThreadPoolExecutor#afterExecute()方法中工作线程抛出的exeptions .目前我有这个代码:
public class MyExecutor extends ThreadPoolExecutor {
public static void main(String[] args) {
MyExecutor threadPool = new MyExecutor();
Task<Object> task = new Task<>();
threadPool.submit(task);
}
public MyExecutor() {
super(4, 20, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(4000));
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
System.out.println("in afterExecute()");
if (t != null) {
System.out.println("exception thrown: " + t.getMessage());
} else {
System.out.println("t == null");
}
}
private static class Task<V> implements Callable<V> {
@Override
public V call() throws …Run Code Online (Sandbox Code Playgroud) java multithreading futuretask threadpool threadpoolexecutor
我有一个简单的java ExecutorService运行一些任务对象(实现Callable).
ExecutorService exec = Executors.newSingleThreadExecutor();
List<CallableTask> tasks = new ArrayList<>();
// ... create some tasks
for (CallableTask task : tasks) {
Future future = exec.submit(task);
result = (String) future.get(timeout, TimeUnit.SECONDS);
// TASKS load some classes and invoke their methods (they may create additional threads)
// ... catch interruptions and timeouts
}
exec.shutdownNow();
Run Code Online (Sandbox Code Playgroud)
在完成所有任务(DONE或TIMEOUT-ed)之后,我尝试关闭执行程序,但它不会停止:exec.isTerminated() = FALSE.
我怀疑某些被执行的任务未正确终止.
是的,我知道执行者的关闭不能保证任何事情:
除尽力尝试停止处理主动执行任务之外,没有任何保证.例如,典型的实现将通过{@link Thread#interrupt}取消,因此任何未能响应中断的任务都可能永远不会终止.
我的问题是,有没有办法确保这些(任务)线程终止?我提出的最佳解决方案是System.exit()在程序结束时调用,但这很简单.
在我的程序中,我从一个可运行的程序中创建了一个断言 - 其值为false - 但从未看到有关断言的任何控制台输出.我想知道我的断言是否为假,但似乎runnable正在捕获所有抛出的断言?
下面是我可以编写的最简单的示例程序来演示.(断言已启用.如果不是,则程序的行为会有所不同,并打印两行而不是一行).该程序的输出是.
即将断言False
而已.在那之后,断言语句抛出并被某些东西捕获,我从来不知道它.我想知道它,我做错了什么?
import java.nio.ByteBuffer;
import java.util.concurrent.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.*;
class App
{
private static final ScheduledExecutorService sExecutor =
Executors.newSingleThreadScheduledExecutor();
// Main
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { createAndShowGUI(); } });
}
// Swing GUI
private static void createAndShowGUI()
{
// Just create a swing thing. Boring
JFrame frame = new JFrame("Title String");
JLabel label = new JLabel("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setLayout(new FlowLayout()); …Run Code Online (Sandbox Code Playgroud) 我有一个主线程,它创建了许多工人,每个工人都是一个线程.
如果某个工作者或工作者中的异常无法成功结束,如何从主线程中的工作者获取错误?
如何在工作线程死之前发送错误?