Writing High-Performance Applications With the Java Foundation Classes (JFC/Swing) API
Steve Wilson
Java Performance Engineering
Sun Microsystems
Introduction
The JFC/Swing API has a reputation
for being slow
It doesnt have to be
Well written JFC/Swing GUIs can be
highly interactive and responsive
Presentation Goal
Understand the key concepts required to construct fast, responsive graphical user interfaces using the JFC/Swing toolkit
Overview
What is Performance?
High-Level Strategies
Low-Level Tactics
- Model/View/Controller (MVC)
- Multi-threaded GUIs
- Deployment options
The Book
What Is Performance?
Computational Performance
RAM Footprint
Startup Time
Scalability
Perceived Performance
Performance Process Diagram
Profiling Tools
Hot methods
Call stacks
Memory allocations
- Allocation takes time
- Track memory leaks
- Decrease footprint
Commercial Profiling Tools
Excellent commercial tools are available:
- OptimizeIt from VM Gear
- JProbe from Sitraka
- TrueTime from Compuware
See session BUS-574 for more info
Tactics Overview
Little Objects
Model/View/Controller (MVC)
Responsiveness (Multi-Threaded GUIs)
Deployment
A new goodie in the J2SE 1.4 release
Object Allocation Costs
Classic VM (1.1.x), costs are significant
for all objects
Java HotSpot VM costs are much lower
for small short-lived objects
You still want to avoid thrashing the GC
Remember initialization costs
Returned Copies
Mutable objects often copied to ensure encapsulation
MVC and Performance
Historically praised for reusability
and maintainability
Also helps performance!
Move control of data into app space
You determine optimal handling for:
- Allocation
- Representation
- Binding between data and model
JFC/Swing APIs
Model/Renderer System
Loading a ComboBox
Two ways to accomplish task
One is fast, one is slow
JComboBox: Try #1
Takes 1000 milliseconds
Doesnt scale well
Constantly firing update events
JComboBox: Try #2
Takes only 50 milliseconds
Scales well
Prototype Cells
JList and JComboBox must scan all entries to determine proper size for layout
This can be slow
Can use new methods to short-cut this for the case where know the largest entry
- JList.setPrototypeCellValue(Object)
- JComboBox.setPrototypeDisplayValue(Object)
Prototype Cell Example
Prototype Cell Performance
Time to display Component with 10,000 items in
milliseconds on 866 MHz PC
Small numbers are good
TableModel and Footprint
JTable can display large amounts of data
How best to store it
You can decide based on your problem!
Big Model (Simple)
Uses 4MB before first cell is set
Big Model (Sparse)
Uses about 1KB before first cell is set
JFC/Swing APIs Default Models
Simple
General Purpose
Not highly tuned for your app
For large data sets youll usually
want a custom model
Multi-threaded GUIs
JFC/Swing technology is single-threaded
You can use multiple threads in a
JFC/Swing API-based app
You just need to follow the rules
Effective use of threading is required to
build complex, responsive GUIs
Responsiveness
User actions produce immediate feedback
Let them know whats going on
Let them interrupt whats going on
Let them know why theyre waiting
JFC/Swing API and Threading
Responsiveness often requires multi-threading
You need to understand the JFC/Swing APIs thread model
JFC/Swing APIs Thread Model
JFC/Swing API-based UIs are single threaded
Most actions that touch GUI must take place
on the AWT event thread
Events occur on event thread
Painting occurs on the event thread
Life is usually simple!
Single Thread Rule
Touch JComponents only from AWT event thread after theyre realized
- setVisible(true)
- show()
- pack()yes pack counts too!
Construct GUI on any thread
Exceptions to the Rule
These methods are safe from any thread:
- repaint()
- invalidate()
- revalidate()
Other methods as marked
Otherwise
- invokeLater()
- invokeAndWait()
When to Use Threads?
Long or indeterminate tasks
Repetitive tasks
Long Tasks
Very complex calculation
Big database query
Anything that touches the network
Anything that can block!
Dont block the event thread!
Example #1: DB Query
User Clicks JButton
Event comes to listener on event thread
You do query (on event thread) <- STOP
Event thread waits
Results return
Update GUI (on event thread)
Example #2: Better Query
User Clicks JButton
Event comes to listener on event thread
You do query (on a new thread) <- BETTER
Event thread continues
Results return
Update GUI (with invokeLater)<- BETTER
Using SwingWorker
Get class from JFC/Swing Connection
Repetitive Actions
Polling for a condition
Doing an animation
Use javax.swing.Timer
There is also java.util.Timer
Deployment
JAR Files
Java Plug-In technology
Java Web Start software
Java Archives
JAR files package network deployed code
and resources
Major improvements in download time
SwingSet Example on LAN:
Sticky JARs
New for 1.3, control of applet caching in plug-in
http://java.sun.com/j2se/1.3/docs/guide/misc/
appletcaching.html
Java Web Start Software
Java Web Start software allows for
sticky applications
Also allows for application components
to be downloaded on demand
http://java.sun.com/products/javawebstart
Volatile ImageNew for 1.4
New Java 2D API VolatileImage class
Allows the Java 2D API to use hardware accelerated video
JFC/Swing APIs double-buffer tweaked
to use
Makes big performance improvement
for some apps
See session TS-1314 for more details
Summary
Understand JFC/Swing APIs MVC architecture
JFC/Swing APIs default renderers are
not tuned for your app
Use multi-threading to improve responsiveness
Packaging matters
Performance improvements coming in 1.4