I'm investigating a deadlock problem on setText but I need to first learn and understand about deadlocks. To this end I have created a short program to try and replicate what may be happening on the larger scale but I am unsure as to why my smaller program never deadlocks.
Here is my learning program:
public static void main(String[] a)
{
JFrame frame = new JFrame();
final JTextField p = new JTextField("start");
JButton btn = new JButton("button");
btn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
p.setText(String.valueOf(System.nanoTime()));
}
});
}
});
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(p);
frame.getContentPane().add(btn);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
I thought that modifications to swing could not be done in a separate thread, so I have a setText to change the JTextField on a button click in a invokeLater
. Doing so should break the single thread rule, would this not cause a deadlock?