实施细节:我正在做一个学校项目,我必须模拟一些队列。应随机生成客户端,客户端选择一个队列(我可以有多个队列)进入,并添加到该队列数据结构中。每个队列都有自己的操作符,可以从它所连接的队列中删除客户端。
问题:客户端生成器在单独的线程中运行。队列图形表示是 JButton 的 ArrayList,显示在 GridLayout 面板上,只有 1 列。当我尝试将客户端(一个 JButton)添加到面板时,我想使用 SwingWorker 的 publish() 发布一个新的 JButton,以添加到列表中。然而,经过一番头疼,以及 System.out.println 弄清楚发生了什么之后,我观察到 process() 方法中的 System.out.println() 仅在 doBackground() 方法完成后才被调用。
代码在这里:
//run method of the ClientGenerator thread
public void run()
{
System.out.println("Into thread Generator");
SwingWorker<Void,JButton> worker=new SwingWorker<Void, JButton>()
{
int sleepTime;
@Override
protected Void doInBackground() throws Exception
{
while(checkTime())
{
try
{
sleepTime=minInterval+r.nextInt(maxInterval - minInterval);
System.out.println("Sleeping - "+sleepTime+" milis");
Thread.sleep(sleepTime);
System.out.println("Woke up,"+sleepTime+" milis elapsed");
} catch (InterruptedException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println("Generating client...");
newClient=new Client(clientMinService,clientMaxService,log);
System.out.println("Locking lock...");
operationsOnTheQueueLock.lock();
selectedQueueOperator=selectQueueOperator();
System.out.println("Adding new client to queue...");
selectedQueueOperator.getQueue().enqueue(newClient);
System.out.println("Publishing new JButton...");
publish(new JButton("C"+selectedQueueOperator.getClientIndicator()));
//}
// else
// {
// queueHolder.add(selectedQueueOperator.getQueueClients().get(0);
// }
System.out.println("Unlocking lock...");
operationsOnTheQueueLock.unlock();
System.out.println("Lock unlocked! Should enter while again and sleep");
}
return null;
}
@Override
public void process(List<JButton> chunks)
{
newClientButton=chunks.get(chunks.size()-1);
System.out.println("Process runs.Jbutton index="+newClientButton.getText());
newClientButton.setFont(new Font("Arial", Font.PLAIN, 10));
newClientButton.setBackground(Color.lightGray);
newClientButton.setVisible(true);
newClientButton.setEnabled(false);
clients=selectedQueueOperator.getQueueClients();
clients.add(newClientButton);
selectedQueueOperator.setQueueClients(clients);
// if(selectedQueueOperator.getQueueClients().size()>0)
// {
queueHolder=selectedQueueOperator.getQueueHolder();
queueHolder.add(clients.get(clients.size()-1));
selectedQueueOperator.setQueueHolder(queueHolder);
}
// return null; //To change body of implemented methods use File | Settings | File Templates.
};
worker.execute();
try {
worker.get();
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ExecutionException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
输出:
Sleeping - 1260 milis
Woke up,1260 milis elapsed
Generating client...
Locking lock...
Adding new client to queue...
Publishing new JButton... ///here I should see "Process runs.Jbutton index=C0"
Unlocking lock...
Lock unlocked! Should enter while again and sleep
Sleeping - 1901 milis
Woke up,1901 milis elapsed
Generating client...
Locking lock...
Adding new client to queue...
Publishing new JButton...///here I should see "Process runs.Jbutton index=C1
Unlocking lock...
Lock unlocked! Should enter while again and sleep
Process runs.Jbutton index=C0 //instead, Process runs only in the end.
这只是一个基本示例,用于 2 次迭代。客户端应该只是不时生成,所以一开始我让线程休眠一段时间。然后我生成客户端对象,然后我想在 process() 方法中生成按钮并将其添加到我的 JPanel 组件中。
最后一部分,显然没有发生。任何想法为什么?关于 SwingWorker,我没有办法尝试...
提前致谢!
稍后编辑:“锁定”定义为:
Lock lock = new ReentrantLock();
并作为参数从管理我的 ClientsGenerator(this) 类的类和从队列中删除客户端的类传递。在对 ArrayList& 显示执行操作时,它用于同步两者。