Swing Threading 101: An Introduction to the Event Dispatch Thread

lightnewsΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

117 εμφανίσεις

2006 JavaOne
SM
Conference | Session TS-4855 |
Swing Threading 101:
An Introduction to the
Event Dispatch Thread
Scott Delap
Rich Client Consultant
Scott Gelb
Enterprise Consultant
TS-4855
2006 JavaOne
SM
Conference | Session TS-4855 |
2
About Us

Scott Delap

Editor, ClientJava.com

Author, Desktop Java Live

Rich Client Java™
Technology Consultant

Swing

Eclipse RCP

Ajax

Scott Gelb

Enterprise Java Technology
Consultant

Member of the Java
Community
Process
SM
services
2006 JavaOne
SM
Conference | Session TS-4855 |
3
Learn best practices for creating
responsive rich client applications
in Swing using threads
Goal
2006 JavaOne
SM
Conference | Session TS-4855 |
4
Agenda
Introduction
SwingUtilities
Structured Threading Solutions
Debugging Techniques
Using AOP for Safeguards
Final Thoughts
2006 JavaOne
SM
Conference | Session TS-4855 |
5
Agenda
Introduction
SwingUtilities
Structured Threading Solutions
Debugging Techniques
Using AOP for Safeguards
Final Thoughts
2006 JavaOne
SM
Conference | Session TS-4855 |
6
OS, Java Technology, and Swing Threads
Introduction
2006 JavaOne
SM
Conference | Session TS-4855 |
7
Thread Interaction
Introduction
2006 JavaOne
SM
Conference | Session TS-4855 |
8
Swing Single Threading Rule
Introduction

The first version…


Once a Swing component has been realized

all code that might affect or depend on the
state of that component should be executed
in the event-dispatching thread.”
2006 JavaOne
SM
Conference | Session TS-4855 |
9
Swing Single Threading Rule
Introduction

The rule today…


All code that might affect or depend on the state
of that component should be executed in the
event-dispatching thread.”
2006 JavaOne
SM
Conference | Session TS-4855 |
10
There Are a Few Thread Safe Methods…
Introduction

revalidate()

invalidate()

repaint()

add*Listener()

remove*Listener()

Read the Java Docs
2006 JavaOne
SM
Conference | Session TS-4855 |
11
Why Single Threading Is Good
Introduction

Developers don’t need an in-depth
understanding of threads

Events are dispatched in a predictable order

Less overhead

Same approach used by other UI toolkits
2006 JavaOne
SM
Conference | Session TS-4855 |
12
Why Single Threading Is Bad
Introduction
2006 JavaOne
SM
Conference | Session TS-4855 |
13
DEMO
Why Single Threading Is Bad
2006 JavaOne
SM
Conference | Session TS-4855 |
14
Common Causes of Single Threading Problems
Introduction

Long running event listeners

Socket communication

Processing expensive algorithms

Non UI-related work
2006 JavaOne
SM
Conference | Session TS-4855 |
15
Solving Single Threading Problems
Introduction

Keep your event listeners lightweight

Multi-thread your application

Doesn’t this violate the single threading rule?
2006 JavaOne
SM
Conference | Session TS-4855 |
16
Agenda
Introduction
SwingUtilities
Structured Threading Solutions
Debugging Techniques
Using AOP for Safeguards
Final Thoughts
2006 JavaOne
SM
Conference | Session TS-4855 |
17
Introduction
SwingUtilities

Move work to a new thread

Releases the Event Dispatch Thread (EDT)
to paint, respond to mouse events, etc.

What if you need to update the GUI?
2006 JavaOne
SM
Conference | Session TS-4855 |
18
Threading Helper Methods
SwingUtilties

Swing provides two static methods to assist
in multi-threading your application
SwingUtilities.invokeLater(Runnable)
SwingUtilities.invokeAndWait(Runnable)

These methods place the
Runnable
operations
in the event queue for processing on the EDT
2006 JavaOne
SM
Conference | Session TS-4855 |
19
invokeLater()
SwingUtilities
2006 JavaOne
SM
Conference | Session TS-4855 |
20
DEMO
Fixing Swing Threading Issues
With SwingUtilities.invokeLater()
2006 JavaOne
SM
Conference | Session TS-4855 |
21
invokeAndWait()
SwingUtilities
2006 JavaOne
SM
Conference | Session TS-4855 |
22
Using InvokeAndWait
SwingUtilities

Blocks calling thread until event queue
processes
Runnable

Useful for displaying modal dialogs

Can cause deadlocks

Use with caution
2006 JavaOne
SM
Conference | Session TS-4855 |
23
Agenda
Introduction
SwingUtilities
Structured Threading Solutions
Debugging Techniques
Using AOP for Safeguards
Final Thoughts
2006 JavaOne
SM
Conference | Session TS-4855 |
24
Structured Threading Solutions

Asynchronous

SwingWorker

Synchronous

Foxtrot
2006 JavaOne
SM
Conference | Session TS-4855 |
25
SwingWorker
Structured Threading Solutions

Has moved frequently

Originally a source download of an article

Included as part of the JDNC Project

Included in Java SE 6

Java.net standalone project that backports
to Java SE 5
2006 JavaOne
SM
Conference | Session TS-4855 |
26
SwingWorker (Cont.)
Structured Threading Solutions

Simplifies multi-threading process

Span new thread

Perform work

Update UI on EDT

Makes use of java.util.concurrent
2006 JavaOne
SM
Conference | Session TS-4855 |
27
DEMO
Fixing Swing Threading Issues
With SwingWorker
2006 JavaOne
SM
Conference | Session TS-4855 |
28
SwingWorker Animation
Structured Threading Solutions
2006 JavaOne
SM
Conference | Session TS-4855 |
29
Is There a Simpler Solution?
Structured Threading Solutions

SwingWorker is asynchronous

Asynchronous programming can be complex

Is there an easier way to simply “not tie up”
the EDT?

How do modal dialogs work?
2006 JavaOne
SM
Conference | Session TS-4855 |
30
Modal Dialog Animation
Structured Threading Solutions
2006 JavaOne
SM
Conference | Session TS-4855 |
31
DEMO
Fixing Swing Threading Issues With Foxtrot
2006 JavaOne
SM
Conference | Session TS-4855 |
32
Foxtrot Animation
Structured Threading Solutions
2006 JavaOne
SM
Conference | Session TS-4855 |
33
Agenda
Introduction
SwingUtilities
Structured Threading Solutions
Debugging Techniques
Using AOP for Safeguards
Final Thoughts
2006 JavaOne
SM
Conference | Session TS-4855 |
34
Is There an Easy Way to Find Violations?
Debugging Techniques

Audit all threads/runnables

Finding the hotspots

Many Swing calls cause repaints

Have the repaint manager check the calling thread

CheckThreadViolationRepaintManager

Available on Java.net

https://swinghelper.dev.java.net
2006 JavaOne
SM
Conference | Session TS-4855 |
35
DEMO
Debugging Swing Threading With a Custom
RepaintManager
2006 JavaOne
SM
Conference | Session TS-4855 |
36
Agenda
Introduction
SwingUtilities
Structured Threading Solutions
Debugging Techniques
Using AOP for Safeguards
Final Thoughts
2006 JavaOne
SM
Conference | Session TS-4855 |
37
Can AOP Assist With Swing Threading?
Using AOP for Safeguards

Aspect-oriented programming

Allows interception of method calls

Violations can be found

Optionally they can be automatically fixed

Your application is only as safe as your
AOP advice!

2006 JavaOne
SM
Conference | Session TS-4855 |
38
DEMO
Finding and Resolving Swing Threading
Issues With AOP
2006 JavaOne
SM
Conference | Session TS-4855 |
39
Agenda
Introduction
SwingUtilities
Structured Threading Solutions
Debugging Techniques
Using AOP for Safeguards
Final Thoughts
2006 JavaOne
SM
Conference | Session TS-4855 |
40
Final Thoughts

Only multi-thread when necessary

Observe the single threading rule and audit
your Application regularly for violations

Leverage built-in and open source tools to
make threading easier

SwingUtilities

SwingWorker

Foxtrot
2006 JavaOne
SM
Conference | Session TS-4855 |
41
For More Information

SwingWorker

https://swingworker.dev.java.net/

Foxtrot

http://foxtrot.sourceforge.net/

Spin

http://spin.sourceforge.net

Desktop Java Live Threading Chapter

http://www.sourcebeat.com/TitleAction.do?id=10

AspectJ and Swing Threading

http://www.manning.com

Swing Debugging BOF

BOF-0204:
https://swinghelper.dev.java.net
2006 JavaOne
SM
Conference | Session TS-4855 |
42
Q&A
2006 JavaOne
SM
Conference | Session TS-4855 |
Swing Threading 101:
An Introduction to the
Event Dispatch Thread
Scott Delap
Rich Client Consultant
Scott Gelb
Enterprise Consultant
TS-4855