我有3个摇摆工人。和4个jButton。
jButton1必须分配给Swingworkers的排队工作。单击后,swingworker必须排队并一一执行。我知道我们可以使用执行器。我读了很多有关它的文章,但是人们通常会从第一个swingworker done()方法调用执行程序。我做不到,因为;
jButton2,jButton3,jButton4必须分配给swingworker才能执行。我们如何创建这种情况?任何示例架构表示赞赏。
问候。
- -编辑 - -
MainGui看起来像下面的ss;
我所有的Swingworkers都存放在其他班级;
package trying.project;
import java.awt.Toolkit;
import javax.swing.SwingWorker;
public class WorkersClass {
public class SW1 extends SwingWorker<Void, Void> {
protected Void doInBackground() throws Exception {
//Doing Some DB Jobs Here...
return null;
}
public void done() {
Toolkit.getDefaultToolkit().beep();
}
}
public class SW2 extends SwingWorker<Void, Void> {
protected Void doInBackground() throws Exception {
//Doing Some DB Jobs Here...
return null;
}
public void done() {
Toolkit.getDefaultToolkit().beep();
}
}
public class SW3 extends SwingWorker<Void, Void> {
protected Void doInBackground() throws Exception {
//Doing Some DB Jobs Here...
return null;
}
public void done() {
Toolkit.getDefaultToolkit().beep();
}
}
}
所有SW按钮必须按顺序并一一执行所有摆动工作人员。 SW1比SW2优先于SW3。
SW 1按钮只能执行SW1,
SW 2按钮只能执行SW2,
SW 3按钮只能执行SW3。
最佳答案
我一直在玩这个问题一段时间(如果我正确理解的话),这是我找到的最简单的方法。我制作了两个类WorkerManager
和QueuableWorker
,它们可以共同完成以下任务:
不能同时执行两个QueuableWorker
工人必须按入队顺序执行
以下测试应用程序具有3个按钮,可创建不同的工作程序。无论您要求执行的速度有多快,工作人员都不会重叠,并且会按顺序执行。控制台中的消息显示何时对工作人员进行排队,开始和完成。
要执行queuableWorker,请调用WorkerManager的方法queueExecution(worker)
,并覆盖QueuableWorker的customDone
方法,以在执行后将自己的代码放入EDT。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class QueueSWTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
new QueueSWTest();
}
});
}
static WorkerManager _workerManager;
public QueueSWTest()
{
_workerManager = new WorkerManager();
JFrame frame = new JFrame("Frame");
JButton button1 = new JButton("Queue TestWorker 1");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
String workerName = "Worker 1";
appendMessage("Queued " + workerName);
TestWorker worker = new TestWorker(workerName);
_workerManager.queueExecution(worker);
}
});
JButton button2 = new JButton("Queue TestWorker 2");
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
String workerName = "Worker 2";
appendMessage("Queued " + workerName);
TestWorker worker = new TestWorker(workerName);
_workerManager.queueExecution(worker);
}
});
JButton button3 = new JButton("Queue TestWorker 3");
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
String workerName = "Worker 3";
appendMessage("Queued " + workerName);
TestWorker worker = new TestWorker(workerName);
_workerManager.queueExecution(worker);
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(button1);
buttonsPanel.add(button2);
buttonsPanel.add(button3);
frame.add(buttonsPanel, BorderLayout.PAGE_END);
_textArea = new JTextArea("Queue some workers:\n");
_textArea.setEditable(false);
frame.add(new JScrollPane(_textArea));
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static WorkerManager getWorkerManager()
{
return _workerManager;
}
private abstract class QueuableWorker extends SwingWorker
{
@Override
protected abstract Object doInBackground();
@Override
final protected void done()
{
customDone();
QueueSWTest.getWorkerManager().setExecuting(false);
QueueSWTest.getWorkerManager().executeNext();
}
protected abstract void customDone();
}
private class TestWorker extends QueuableWorker
{
public TestWorker(String name)
{
_name = name;
}
@Override
protected Object doInBackground()
{
doHardWork();
return null;
}
@Override
protected void customDone()
{
String message = _name + " has finished!";
appendMessage(message);
}
public String getName()
{
return _name;
}
private void doHardWork()
{
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
String _name;
}
private static void appendMessage(String message)
{
_textArea.append(message + "\n");
System.out.println(message);
}
class WorkerManager
{
volatile Queue<QueuableWorker> _workers;
volatile boolean _executing;
public WorkerManager()
{
_workers = new LinkedList<QueuableWorker>();
}
public synchronized void queueExecution(QueuableWorker worker)
{
_workers.add(worker);
if (!_executing) executeNext();
}
public synchronized void executeNext()
{
SwingWorker worker = _workers.poll();
if (worker != null)
{
setExecuting(true);
appendMessage("WorkerManager: Starting the next worker... (" + _workers.size() +" more queued)");
worker.execute();
}
}
public void setExecuting(boolean b)
{
_executing = b;
}
}
private static JTextArea _textArea;
}
看起来像这样: