Javadoc的done()方法SwingWorker:
在doInBackground方法完成后在Event Dispatch Thread上执行.
我已经找到了在取消工人的情况下不是这样的线索.
Done在每种情况下都会被调用(正常终止或取消),但是当cancelled它没有排入 EDT时,就像正常终止时那样.
done在SwingWorker取消a的情况下调用时是否有一些更精确的分析?
澄清:这个问题不是关于如何做到cancel的SwingWorker.这里假设SwingWorker以正确的方式取消.
而且当它们应该完成时,它不是关于线程仍在工作.
我试图在Swing中触及MVC架构的限制,但是当我尝试所有(来自SwingWorker或Runnable#Thread)在EDT上完成时
我的问题:
是否有一些限制或严格依赖于实现的顺序(包装SwingWorker或Runnable#Thread)?
有限的是JComponent#方法线程安全吗?
Swing中MVC架构的本质特征是什么?
INC.集装箱重新布局?
注意:对于我,SSCCE我采取了一个很好的例子HFOE,并且可能通过严格控制这个原则是不可能创建任何EDT缺乏或GUI冻结

import java.awt.BorderLayout;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;
public class MVC_ProgressBarThread {
private MVC_ProgressBarThread() {
MVC_View view = new MVC_View();
MVC_Model model = new MVC_Model();
MVC_Control control = new MVC_Control(view, model);
view.setControl(control);
JFrame frame = new JFrame("MVC_ProgressBarThread");
frame.getContentPane().add(view);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码片段:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
public class TestApplet extends JApplet
{
@Override
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
@Override
public void run()
{
createGUI();
}
});
}
catch(InterruptedException | InvocationTargetException ex)
{
}
}
private void createGUI()
{
getContentPane().setLayout(new FlowLayout());
JButton startButton = new JButton("Do work");
startButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JLabel label = new JLabel();
new Worker(label).execute();
}
});
getContentPane().add(startButton);
}
private class Worker extends SwingWorker<Void, …Run Code Online (Sandbox Code Playgroud) 我一直在努力解决SwingWorker的可用性问题,吃掉后台任务中抛出的任何异常,例如,在这个SO线程中描述的.该线程给出了一个很好的问题描述,但没有讨论恢复原始异常.
我已被传递的applet需要向上传播异常.但我甚至无法抓住它.我正在使用此博客条目中的SimpleSwingWorker包装类来尝试解决此问题.这是一个相当小的课程,但我会在这里重新发布它仅供参考.
调用代码看起来很像
try {
// lots of code here to prepare data, finishing with
SpecialDataHelper helper = new SpecialDataHelper(...stuff...);
helper.execute(); // this will call get+done on the actual worker
} catch (Throwable e) {
// used "Throwable" here in desperation to try and get
// anything at all to match, including unchecked exceptions
//
// no luck, this code is never ever used :-(
}
Run Code Online (Sandbox Code Playgroud)
包装纸:
class SpecialDataHelper extends SimpleSwingWorker {
public SpecialDataHelper (SpecialData sd) …Run Code Online (Sandbox Code Playgroud) 我正在使用SwingWorker在我正在制作的应用程序上执行一些重载任务.虽然今天我遇到了Executor类和这个例子:
Executors.newCachedThreadPool().execute(new Runnable() {
public void run() {
someTask();
});
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会使用SwingWorker而不是上面的例子吗?这是我目前使用SwingWorker的方式:
SwingWorker worker = new SwingWorker() {
protected Object doInBackground() {
someTask();
return null;
}
};
worker.execute();
Run Code Online (Sandbox Code Playgroud) 我想等待我的SwingWorker完成工作,然后我想执行另一个SwingWorker.在这种情况下,Encrypteer3是一个扩展SwingWorker的类.
我的代码:
input = txtTekst.getText();
key = txtKey.getText();
System.out.println("thread1 start");
Encrypteer3 a = new Encrypteer3();
a.execute();
while(a.isDone()==false){
// do nothing
}
input = output;
key = txtKey1.getText();
System.out.println("thread2 start");
Encrypteer3 b = new Encrypteer3();
b.execute();
while(b.isDone()==false){
// do nothing
}
Run Code Online (Sandbox Code Playgroud)
这使我的GUI冻结并使用了大量的CPU(java在执行时使用了大约95%的CPU).我认为问题是它不断检查线程是否已完成,这是什么使它如此CPU密集.
SwingWorker类上没有join()方法.如何减少CPU占用?
可能这是微不足道的,我很难理解SwingWorker上的简单文档.
这是复制粘贴的内容
工作流程
SwingWorker的生命周期涉及三个线程:
当前线程:在此线程上调用execute()方法.它安排SwingWorker在工作线程上执行并立即返回.可以等待SwingWorker使用get方法完成.
工作线程:在此线程上调用doInBackground()方法.这是所有背景活动应该发生的地方.要通知PropertyChangeListeners有关绑定属性的更改,请使用firePropertyChange和getPropertyChangeSupport()方法.默认情况下,有两个绑定属性:状态和进度.
事件调度线程:此线程上发生所有与Swing相关的活动.SwingWorker调用process和done()方法并通知此线程上的任何PropertyChangeListeners.
通常,Current线程是Event Dispatch Thread.
-
工作线程不是EDT,因此doInBackground()中的代码不能访问GUI元素.我的理解是否正确?
背景:我们有使用SwingWorker的小代码,但有doInBackground()创建FileChooser并调用setCurrentDirectory().我怀疑这导致我异常几乎与 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6637181(11-Closed,not a defect)相同
关于我的问题(可能),我发现了另一种异常类型,我无法从SwingWorker线程中捕获和打印出来.
如何生成RepaintManager异常?
我读这CheckThreadViolationRepaintManager和该方法通过Alexander Potochkin,但似乎没有解决我的问题.
我正在尝试在GUI中实现Swing worker.目前我有一个包含按钮的JFrame.按下此按钮时,它应更新显示的选项卡,然后在后台线程中运行程序.这是我到目前为止所拥有的.
class ClassA
{
private static void addRunButton()
{
JButton runButton = new JButton("Run");
runButton.setEnabled(false);
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
new ClassB().execute();
}
});
mainWindow.add(runButton);
}
}
class ClassB extends SwingWorker<Void, Integer>
{
protected Void doInBackground()
{
ClassC.runProgram(cfgFile);
}
protected void done()
{
try
{
tabs.setSelectedIndex(1);
}
catch (Exception ignore)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白我怎么能传递我的cfgFile对象.有人可以就此提出建议吗?
考虑以下代码:
SwingWorker<Void, Void> sworker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
try {
for (int j = 0; j < 5; j++) {
Callable<Object> worker = new MyCallableImpl();
Future<Object> future = executor.submit(worker);
array[j] = future.get();
}
} catch (InterruptedException e) {
// some code here
} catch (ExecutionException e) {
// some code here
}
// some code here
executor.shutdown();
return null;
}
};
sworker.execute();
Run Code Online (Sandbox Code Playgroud)
正如我在标题中所说:在SwingWorker的doInBackground()方法中调用ExecutorService是一个好习惯吗?它适用于我(JDK1.7),GUI没有被阻止,Executor池中的多个线程在后台运行,但我仍然有些疑惑......
java ×10
swing ×10
swingworker ×6
concurrency ×2
cancellation ×1
exception ×1
jcomponent ×1
wait ×1