Interface SecondaryLoop
-
public interface SecondaryLoopA helper interface to run the nested event loop.
Objects that implement this interface are created with the
EventQueue.createSecondaryLoop()
method. The interface provides two methods,enter()
andexit()
, which can be used to start and stop the event loop.When the
enter()
method is called, the current thread is blocked until the loop is terminated by theexit()
method. Also, a new event loop is started on the event dispatch thread, which may or may not be the current thread. The loop can be terminated on any thread by calling itsexit()
method. After the loop is terminated, theSecondaryLoop
object can be reused to run a new nested event loop.A typical use case of applying this interface is AWT and Swing modal dialogs. When a modal dialog is shown on the event dispatch thread, it enters a new secondary loop. Later, when the dialog is hidden or disposed, it exits the loop, and the thread continues its execution.
The following example illustrates a simple use case of secondary loops:
SecondaryLoop loop; JButton jButton = new JButton("Button"); jButton.addActionListener(new ActionListener() {
@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 {@Override
public void run() { // Perform calculations doSomethingUseful(); // Exit the loop loop.exit(); } }- Since:
- 1.7
- See Also:
Dialog.show()
,EventQueue.createSecondaryLoop()
,Toolkit.getSystemEventQueue()
-
-
Methods Modifier and Type Method and Description enter Blocks the execution of the current thread and enters a new secondary event loop on the event dispatch thread.exit Unblocks the execution of the thread blocked by theenter()
method and exits the secondary loop.
-