swing worker threads 2 handout
TRANSCRIPT
-
8/2/2019 Swing Worker Threads 2 Handout
1/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
Threads - 2
Radu NicolescuDepartment of Computer ScienceUniversity of Auckland
1 October 2010
1 / 2 3
-
8/2/2019 Swing Worker Threads 2 Handout
2/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
1 Swing and Threads
2 SwingUtilities
3 SwingWorker
4 MySwingWorker
5 IOC
2 / 2 3
-
8/2/2019 Swing Worker Threads 2 Handout
3/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
Swing specific threads
Initial thread: the thread that starts the main()
Event-dispatch thread (EDT, aka GUI thread): executesdrawing and event handling code - should not run
time-consuming tasks Worker (background) threads: for time consuming tasks -
must not interact directly with the GUI
Two high-level solutions for the proper interaction with the
GUI thread Using SwingUtilities
Using SwingWorker
3 / 2 3
-
8/2/2019 Swing Worker Threads 2 Handout
4/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
SwingUtilities.invokeLater(runnable-task)
sometimes invoked by the initial thread, to properly create andstart the GUI, if other statements follow (in the initial thread)
typically invoked from a worker thread
schedules a given task to be asynchronously executed by the
even-dispatch thread and returns immediately (withoutwaiting the actual execution)
S w i n g U t i l i t i e s . i n v o k e L a t e r ( new R u n n ab l e ( ) ) {
p u b l i c v oi d run ( ) {// this code will be executed by the event-dispatch thread,// e.g., to update the GUI
}} ) ;
4 / 2 3
S i d Th d S i U ili i S i W k M S i W k IOC
-
8/2/2019 Swing Worker Threads 2 Handout
5/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
SwingUtilities.invokeAndWait(runnable-task)
invokeAndWait() is similar to invokeLater(), but synchronous,i.e., the calling thread waits until the event-dispatch threadcompletes the given task
5 / 2 3
S i d Th d S i Utiliti S i W k M S i W k IOC
-
8/2/2019 Swing Worker Threads 2 Handout
6/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
SwingWorker
SwingWorker is a class with two faceshttp://www.google.com/images?q=janus+images
A SwingWorker encapsulates:
one method, doInBackground(), which defines the task thatneeds to run by an automatically assigned background thread(no need to create this)
two methods, process() and done(), which will beautomatically executed by the GUI thread (at proper times)
6 / 2 3
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
http://www.google.com/images?q=janus+imageshttp://www.google.com/images?q=janus+images -
8/2/2019 Swing Worker Threads 2 Handout
7/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
SwingWorker
the doInBackground() method will return a final result, and,optionally, publishes, item by item, a sequence of interimobjects
the GUI process() and done() methods are provided to displaythe final result of doInBackground() and the sequence of itspublished interim objects (if any)
To effectively use SwingWorker, you need to subclass it, andoverride the above three methods
7 / 2 3
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
8/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
SwingWorker
Additionally, a SwingWorker can use two methods that helptransfer objects from the background thread to the GUI thread(with no further user intervention):
one method, publish(), which transfers interim objects fromdoInBackground() to process()
one methods, get(), typically run in done(), which picks upthe final object created by doInBackground()
Attention, these two methods are final, i.e. not overridable
8 / 2 3
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
9/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
SwingWorker
SwingWorker is a generic predefined type, SwingWorker,where:
T is the type of the final result of doInBackground() andpicked by get()
V is the type of the interim objects published bydoInBackground() further transferred to process()
9 / 2 3
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
10/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
SwingWorker birds eye view
c l a s s MySwingWorker e x t e n d s SwingWorker {
@ O v e r r i d ep u b l i c T d o I n B a c k g r o u n d ( ) // background thread
// optionally, calls s u p e r . p u b l i s h (V) , repeatedly
// finally, returns a T
@ O v e r r i d ep r ot e c te d v o id p r o c e s s ( L i s t ) // GUI thread
// gets lists of Vs, published by d o I n B a c k g r o u n d ( )
@ O v e r r i d ep r ot e c te d v o id done ( ) // GUI thread
// typically, calls s u p e r . Get ( )// to pick the final T, returned by d o I n B a c k g r o u n d ( )
}10/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
11/23
g g g y g
MySwingWorker example
As an example, consider a class MySwingWorker, where:
T = Double, V = Integer
doInBackground() generates a sequence of Integers, say 0, 1,2, . . . , 20
doInBackground() publishes these Integers as interim values,to process()
finally, doInBackground() returns an average Double, here
10.0 the GUI thread displays all interim and final values
the GUI must remain responsive at all times, even if thebackground process takes a long time to complete
11/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
12/23
g g g y g
MySwingWorker example the GUI
Controls: button BoredButton, button SwingWorker, textareaTextBox
12/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
13/23
MySwingWorker example the GUI
button WorkerButton: starts the background execution of aMySwingWorker, disables the button and returns immediately(to keep the GUI responsive)
button BoredButton: prints Wait!, to indicate that the GUI
is NOT frozen (even if MySwingWorker is executing in thebackground)
textarea TextBox: displays results from MySwingWorker(interim and final) and texts printed via BoredButton
13/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
14/23
MySwingWorker example overall structure
c l a s s MySwingWorkere x t e n d s SwingWorker {
@ O v e r r i d e
p u b l i c D o ub l e d o I n B a c k g r o u n d ( ) { . . . }
@ O v e r r i d ep r ot ec te d v o id p r o c e s s ( L i s t ) { . . . }
@ O v e r r i d ep r ot ec te d v o id done ( ) { . . . }
}
14/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
15/23
MySwingWorker example doInBackground()
p u b l i c D o ub l e d o I n B a c k g r o u n d ( ) {Double avg = 0 . 0 ;i n t k = 0 ;f o r ( i n t i =0; i
-
8/2/2019 Swing Worker Threads 2 Handout
16/23
MySwingWorker example doInBackground()
p u b l i c D o ub l e d o I n B a c k g r o u n d ( ) {. . .
// this artificial sleep favours that published numbers// are grouped together into lists of three
t r y {i f ( i %3==0) Thread . s l e e p ( 1 0 0 0 ) ;
} catch ( I n t e r r u p t e d E x c e p t i o n e x ) {;
}. . .
}
16/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
17/23
MySwingWorker example process()
@ O v e r r i d ep r ot ec t ed v oi d p r o c e s s ( L i s t ){//GUI thread
T ra ce Bo x . a pp en d ( . . . p r o c e s s : ) ;
f o r ( I n t e g e r i : numbers ) {TraceBox . append ( i+ ) ;
}
TraceBox . append ( \ r \n ) ;}
17/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
18/23
MySwingWorker example done()
@ O v e r r i d e
p r ot ec t ed v oi d done ( ) { // GUI threadDouble avg = 0 . 0 ;
t r y {
a v g =t h i s
. g e t ( ) ;} catch ( I n t e r r u p t e d E x c e p t i o n e x ) {;
} catch ( E x e c u t i o n E x c e p t i o n e x ) {;
}
TraceB ox . append ( . . . done : avg=+avg+\ r \n ) ;W o r k er B u t to n . s e t E n a b l e d ( t r u e ) ;
}
18/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
19/23
MySwingWorker example WorkerButton handler
A click on WorkerButton starts the background execution of aMySwingWorker, disables the button and returns immediately (tokeep the GUI responsive):
p u b l i c v oi d W o r k e r B u t t o n C l i c k ( ) { // GUI threadMySwingWorker msw = new MySwingWorker ( ) ;msw . e x e c u t e ( ) ;W o r k er B u t to n . s e t E n a b l e d ( f a l s e ) ;
}
19/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
20/23
MySwingWorker example BoredButton handler
A click on BoredButton prints something, to indicate that the GUIis NOT frozen (even if MySwingWorker is executing in thebackground):
p u b l i c v oi d B o r e d B u t t o n C l i c k ( ) { // GUI threadTraceBox . append ( w a i t !\ r \n ) ;
}
20/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
21/23
Inversion of Control (IOC)
There is a bit of magic, in SwingWorker, this class with twofaces
you define the methods process() and done(),
but you do not invoke them (not directly)
the infrastructure will schedule their execution, in the GUIthread (as appropriate)
This is a nice example of an interesting design pattern, called IOC(Inversion of Control)
21/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
22/23
Inversion of Control (IOC)
Normally
either we call methods already created by others (librarymethods)
or we call methods created by ourselves
In the IOC pattern, the library calls our methods
we create methods that we do not ourselves call (not directly)
the existing infrastructure (library) will automatically call ourmethods, as appropriate
22/23
Swing and Threads SwingUtilities SwingWorker MySwingWorker IOC
-
8/2/2019 Swing Worker Threads 2 Handout
23/23
SwingWorker get() exceptions
get() is blocking call the caller waits until doInBackground()terminates
you need not worry about this, if you call get() in the done()method
however, in general, a waiting get() caller can be interrupted,i.e. can receive an InterruptedException
a waiting get() caller can receive an indication thatdoInBackground() terminated abnormally, via anExecutionException
the ExecutionException can be further investigated (but wedont follow this issue here)
23/23