1 UI application
The user is boss of the application, so the UI thread has highest priority. workerthread has lower priority.
· Keep the priorityof worker threads low.
· Keep the numberof worker threads small.
· Consider suspending worker threads duringCPU-intensive operations like scrolling.
Key Points
- Using threads is essential for building responsive GUIs. Blocking user activity to wait for long tasks to complete leads to poor perceived performance.
- The user is the boss. Always let your users know what's going on and give them regular status updates when waiting for long tasks to complete.
- Once realized, Swing components should only be touched by code executing inside the AWT event-dispatch thread.
- Use invokeLater and invokeAndWait to move work to the event dispatching thread.
- Use timers for repeated operations. You can use either the javax.swing.Timer or java.util.Timer. The utility Timer class gives you more control, but you have to move work to the event- dispatch thread yourself. You can use the SwingTimerTask utility described in this chapter to move work to the event-dispatch thread.
- Use SwingWorker to execute time-consuming tasks on new threads and update the GUI on the event-dispatch thread.
- Interrupt worker threads when the user is driving the system.
2 Real time server application: