We have UI which create multiple swing workers from various action listeners. There is another action listener method like
void actionListener()
{
boolean b = performSomeTask();
if(b)
{
do something
}
}
boolean performTask()
{
wait all until all swing workers complete
(required swing workers are internally maintained)
do something
return some condition
}
We have done this using foxtrot Worker in performTask(). It loops until swingworker count becomes 0 in a foxtrot task. Now the issue a running swingworker try to push task to foxtrot worker but the worker is already accupied. Since foxtrot worker is single threaded it ends up in a dead lock. Tried the ConcurrentWorker but it too does not work.
Is here any other way to achieve this target? Main requirement is peformTask should return some value(listener base solution wont work) and it should not block EDT since swingworkers may push things to EDT before they complete
Following example contains somewhat similar behaviour
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import foxtrot.ConcurrentWorker;
import foxtrot.Task;
import foxtrot.Worker;
public class FoxTrotExample extends JFrame
{
public static void main(String[] args)
{
FoxTrotExample example = new FoxTrotExample();
example.setVisible(true);
}
boolean b = true;
public FoxTrotExample()
{
super("Foxtrot Example");
JTextField txtF = new JTextField();
final JButton alam = new JButton("Alam");
alam.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Worker.post(new Task()
{
public Object run() throws Exception
{
System.out.println("Alam will trigger in 2 sec");
Thread.sleep(5000);
System.out.println("Alam going to ring");
return "";
}
});
System.out.println("Ring");
b = false;
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
final JButton button = new JButton("Take a nap !");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button.setText("Sleeping...");
String text = null;
try
{
text = (String) Worker.post(new Task()
{
public Object run() throws Exception
{
//wait for other task get executed
while (b)
{
System.out.println("In the loop");
Thread.sleep(2000);
}
return "Slept !";
}
});
}
catch (Exception x)
{
x.printStackTrace();
}
button.setText(text);
}
});
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridBagLayout());
c.add(button, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
new Insets(5, 5, 5, 5), 0, 0));
c.add(alam, new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
new Insets(5, 5, 5, 5), 0, 0));
txtF.requestFocusInWindow();
setSize(300, 200);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = getSize();
int x = (screen.width - size.width) >> 1;
int y = (screen.height - size.height) >> 1;
setLocation(x, y);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}