Edit : I have referred this link and I'm able to understand the codeflow of InvokeLater. My question is, why is this logic implemented this way? Are there any specific reasons?
Following is my code:
private void init()
{
JFrame jfr = new JFrame();
jfr.setSize(500, 500);
jfr.setVisible(true);
jfr.setTitle("Test");
JButton jb = new JButton("Ok");
jfr.add(jb);
jb.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
@Override
public void run()
{
System.out.println("hello");
}
});
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
});
First Question (While using InvokeAndWait
):
Why is it implemented in such a way that it throws an InvocationTargetException
when called within EDT Thread?
Second Question (while using InvokeLater
):
Why InvokeLater allows this ?
Well, this is my basic understanding on EDT threads:
InvokeAndWait:
- Places job F into the EDT event queue and waits until the EDT has executed it.
- This call blocks until all pending AWT events(A, B, C, D, E) have been processed and (then) execute F after which the control returns.
- Returns if and only if the job submitted is completed.(Control returns after F is completed.)
- Technically, a Synchronous blocking call.
InvokeLater:
- Places the job F into the EDT Queue , but doesn't wait for its completion.(Basically , a publish and return call).
- If we don't care about the job completion we can use InvokeLater.
- Technically, a Asynchronous non-blocking call.