Keeping your Swing Applications Responsive using Fox Trot

  • Slides: 12
Download presentation
Keeping your Swing Applications Responsive using Fox. Trot and Friends Rob Ratcliff

Keeping your Swing Applications Responsive using Fox. Trot and Friends Rob Ratcliff

Swing’s Thread Model n n Mostly not Thread Safe by design - very few

Swing’s Thread Model n n Mostly not Thread Safe by design - very few thread safe operations All events added to AWT Queue and processed sequentially Events “popped” off queue by dispatcher running in AWT Thread and delivered to registered listeners Long running listener code results in a nonresponsive application

Sequence Diagram of Event Processing

Sequence Diagram of Event Processing

Consequences of direct Swing calls in an alternate thread n n n Deadlock Quirky

Consequences of direct Swing calls in an alternate thread n n n Deadlock Quirky Painting Sluggish application

Alternatives – Worker Thread and Invoke. Later() // some swing code new Thread(new Runnable()

Alternatives – Worker Thread and Invoke. Later() // some swing code new Thread(new Runnable() { public void run() { // long running task run. Swing. Code(); } protected void run. Swing. Code() { Swing. Utilities. invoke. Later(new Runnable(){ public void run() { // run more swing code } } } ). start();

Alternatives – Swing. Worker // some swing code new Swing. Worker() { public Object

Alternatives – Swing. Worker // some swing code new Swing. Worker() { public Object construct() { // long running code } public void finished() { // more swing code } }. start();

JDialog’s Approach n n n Modal dialog blocks AWT Event Thread Creates another thread

JDialog’s Approach n n n Modal dialog blocks AWT Event Thread Creates another thread to dispatch the events to keep the GUI painting Fox. Trot leverages this concept

Fox. Trot Approach n n Similar to JDialog Blocking call, but takes over event

Fox. Trot Approach n n Similar to JDialog Blocking call, but takes over event queue processing Natural Usage: // some swing code try { Object value = Worker. post(new Task() { public Object run() throws Exception { // long running task }}); } catch (Exception e) {// do something} // more swing code

Dynamic Proxy Client n n Wrap long running calls a Dynamic Proxy that wraps

Dynamic Proxy Client n n Wrap long running calls a Dynamic Proxy that wraps calls with Foxtrot Avoid sprinkling Foxtrot calls everywhere Assists developers in not having to worry about this stuff Keep this stuff deep down in your framework

Demo Example

Demo Example

Comments on Dynamic Proxy for Call. Backs n Example Problem – RMI Callback n

Comments on Dynamic Proxy for Call. Backs n Example Problem – RMI Callback n n n Callback calls Swing methods on RMI dispatch thread to update GUI Inevitable that invoke. Later() will be forgotten by some developer on team resulting in an embarassing deadlock! Embed Invoke. Later into a proxy around the callback interface (See Chat. Listener. Notifier. Factory. )

References n n Foxtrot - http: //foxtrot. sourceforge. net/ n Simone Bordet - simone.

References n n Foxtrot - http: //foxtrot. sourceforge. net/ n Simone Bordet - simone. bordet@hp. com Swing. Worker http: //java. sun. com/products/jfc/tsc/articles/threa ds/update. html Threads and Swing http: //java. sun. com/products/jfc/tsc/articles/threa ds/threads 1. html Dyanamic Proxy -