Here is a code snippet from java.awt.SecondaryLoop JavaDoc:
SecondaryLoop loop;
JButton jButton = new JButton("Button");
jButton.addActionListener(new ActionListener() {
{@code @Override}
public void actionPerformed(ActionEvent e) {
Toolkit tk = Toolkit.getDefaultToolkit();
EventQueue eq = tk.getSystemEventQueue();
loop = eq.createSecondaryLoop();
// Spawn a new thread to do the work
Thread worker = new WorkerThread();
* worker.start();
// Enter the loop to block the current event
// handler, but leave UI responsive
** if (!loop.enter()) {
// Report an error
}
}
});
class WorkerThread extends Thread {
{@code @Override}
public void run() {
// Perform calculations
doSomethingUseful();
// Exit the loop
*** loop.exit();
}
}
The code marked with (*) spawns a new thread. Note that (***), which is called on this new thread, can potentially be invoked before the loop is started with (**). This is not a problem in java.awt.Secondary loop, however there's no easy way for developers to detect and resolve this situation.