GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing foxtrot.sourceforge.net — Foxtrot - Easy API for JFC/Swing - Documentation OverviewThe ProblemAsynchronous SolutionSynchronous SolutionsFoxtrotFoxtrot & SwingFoxtrot's synchronous solution: WorkerThe Foxtrot framework is based on a different approach than asynchronous solutions. While a worker thread is still
used to execute time-consuming tasks, SwingUtilities.invokeLater() is not used. This approach is similar to the one used to display modal dialogs in AWT or Swing; unfortunately all classes that allow dialogs to re-route the Event Dispatch Thread inside a listener to continue dequeueing and processing events are private to package java.awt. However, AWT and Swing architects left enough room to achieve exactly the same behavior, just with a little more coding necessary in the Foxtrot implementation. The main idea behind the synchronous solution is to prevent the Event Dispatch Thread from returning from the time-consuming listener, while having the worker thread executing the time consuming code, and the Event Dispatch Thread continuing dequeuing and processing events from the Event Queue. As soon as the worker thread is done with the task execution, the Event Dispatch Thread will resume its execution of the listener method, and eventually return. That's why these solution are synchronous: the code in the event listener and the code of the task are executed sequentially, as the appear in the code. Take a look at the code below that uses the Foxtrot API. Let's concentrate on the button's listener (the actionPerformed() method): the first statement, as in
the freeze example, changes the text of the button and thus posts a repaint event to the queue. Let's compare this solution with the asynchronous ones, to see how it resolves their drawbacks:
Legend
Main Thread Event Dispatch Thread Foxtrot Worker Thread public class FoxtrotExample extends JFrame { public static void main(String[] args) { FoxtrotExample example = new FoxtrotExample(); example.setVisible(true); } public FoxtrotExample() { super("Foxtrot Example"); 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 { Thread.sleep(10000); return "Slept !"; } }); } catch (Exception x) ... button.setText(text); somethingElse(); } }); setDefaultCloseOperation(DISPOSE_ON_CLOSE); Container c = getContentPane(); c.setLayout(new GridBagLayout()); c.add(button); 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); } }
Web Design based on Beautiful Day by Arcsin
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |