我需要提交一些任务,然后等待所有结果,直到所有结果都可用.它们中的每一个都添加了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执行器的指南/书吗?
我的问题是基于理论的问题,但它确实满足了我的特定需求.
当你的SwingWorker抛出a)你可以预料到并且b)需要从中恢复并继续的异常时,你会怎么做,但是你想通知用户这个错误已经发生了?如何获取预期的异常并通知用户而不违反"No Swing code from doInBackground()"规则?
考虑到这个问题,我已经开发了一个SSCCE,我想提出下面的问题.
SSCCE:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class Test {
public static void main(String args[]) {
final JFrame frame = new JFrame();
JButton go = new JButton("Go.");
go.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Task(frame);
}
});
frame.add(go);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
static class Task extends SwingWorker<Void, Void> {
JFrame …Run Code Online (Sandbox Code Playgroud) 编辑:我已经提到了这个链接,我能够理解 InvokeLater 的代码流。我的问题是,为什么这种逻辑以这种方式实现?有什么具体原因吗?
以下是我的代码:
private void init()
{
JFrame jfr = new JFrame();
jfr.setSize(500, 500);
jfr.setVisible(true);
jfr.setTitle("Test");
JButton jb = new JButton("Ok");
jfr.add(jb);
jb.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
@Override
public void run()
{
System.out.println("hello");
}
});
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
});
Run Code Online (Sandbox Code Playgroud)
第一个问题(使用时InvokeAndWait):
为什么它以这样一种方式实现,即InvocationTargetException在 EDT 线程中调用时会抛出一个?
第二个问题(使用时InvokeLater):
为什么 InvokeLater 允许这样做?
好吧,这是我对 EDT 线程的基本理解:
调用和等待:
我有一个JTabbedPane,其选项卡中包含三个表.的JTabbedPane,再次,是在JScrollPane.我想{"Client", "Action", "Location", "Value"}为每个表显示一个修复表头,它遵循模式.当我试图设置标题时ActionListener(见下文),我可以自由地使用其他方法(例如,最初设置标题).
其他SO帖子也解决了同样的问题,但我无法显示表头.唯一有效的方法是在上述表格中添加数据ActionListener.我不明白为什么标题没有显示,因为我将数据以String-matrix的形式与String表示标题的-array一起传递.
首先,这里是关于我的表的代码片段,它在GUI启动时执行(my initialize()-method).
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
tabbedPane.setFont(new Font("Carlito", Font.PLAIN, 13));
tabbedPane.setBackground(Color.WHITE);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tableBefore = new JTable();
tabbedPane.addTab(descrTableBefore, null, tableBefore, null);
tableBefore.setFont(new Font("Carlito", Font.PLAIN, 13));
tableBefore.setFocusable(false);
tableMainTest = new JTable();
tabbedPane.addTab(descrTableMain, null, tableMainTest, null);
tabbedPane.setEnabledAt(1, true);
tableMainTest.setFont(new Font("Carlito", Font.PLAIN, 13));
tableMainTest.setFocusable(false);
tableAfter = new JTable();
tabbedPane.addTab(descrTableAfter, null, tableAfter, null);
tableAfter.setFont(new Font("Carlito", Font.PLAIN, 13)); …Run Code Online (Sandbox Code Playgroud) java ×4
swing ×3
concurrency ×1
edt ×1
jtabbedpane ×1
jtable ×1
jtableheader ×1
swingworker ×1