Title: Object-Oriented Software Engineering
1Object-Oriented Software Engineering
2Contents
- CoordinatesDemo Overview
- Class Diagram
- Race in asynchronous version
- Synchronization with Lock class
- Wait and NotifyAll methods
- Conceptual statemachines
- Get/release cycle
- Thread run statemachine
3CoordinatesDemo Thread Case Study
- Square created by mouse clickSquare oscillates
across grid from side to side. - Square is created and controlled by threadclass
DrawRect. - Races are prevented bysynchronizing threads.
- Synchronization with Lockclass.
Full code available on web site for this lecture
4Inner Classes
Can access all methods of outer class
Gives Synchronization between threads
5Class Diagram
MouseInputListener
CoordinatesDemo
JComponent
Graphics
JFrame
paints with
CoordinateArea
inner classdependency
contained in
1
Thread
inner class dependency
Black trianglesindicate directionto read
association
1
synchronizes
DrawRect
Lock
0...
1
6(No Transcript)
7Race
coordinateArea CoordinateArea
pointPoint
8Synchronization
coordinateArea CoordinateArea
pointPoint
9Lock Class
lock open
lock closed
(see lecture 1 for state machines)
10Lock Java Code
private boolean available true
public synchronized void grab() while
(available false) try wait()
catch (InterruptedException e)
available false
notifyAll()
public synchronized void release() while
(available true) try
wait() catch (InterruptedException e)
available true
notifyAll()
methods are almost identical Only difference is
duality between boolean
11synchronized methods
public synchronized void grab() while
(available false) try wait()
catch (InterruptedException e)
available false
notifyAll()
12wait( ) method
public synchronized void grab() while
(available false) try wait()
catch (InterruptedException e)
available false
notifyAll()
13notifyAll( ) method
public synchronized void grab() while
(available false) try wait()
catch (InterruptedException e)
available false
notifyAll()
14grab() conceptual statemachine
entry state when thread callsmethod this is
initial state
exit stateafter this state the method returns
available true / available false
wait
notifyAll
available false
15release() conceptual statemachine
entry state when thread callsmethod this is
initial state
exit stateafter this state the method returns
available false / available true
wait
notifyAll
available true
16Sequence Diagram for get/release cycle
thread 2
available true
available false
wait( )
thread becomes inactive
available true
notifyAll( )
return from wait
return from grab
17DrawRect Class
DrawRect public int x public int y private void
grow () private void shrink () public void run()
18grow( ) method
private void grow () point.x x
point.y y try rect_size 4
rect_ofset 2 Double new_y
for (int i 0 i lt itr_rect i)
Thread.currentThread().sleep(50) rect_siz
e 2 rect_ofset 1 repaint()
catch (InterruptedException exp1)
System.out.println("OOOOPS "
exp1.toString())
Causes shape to appear when thread is started,
and when it repeats cycle
19shrink( ) method
private void shrink () point.x x
point.y y try while (rect_size gt 2)
Thread.currentThread().sleep(60) repaint(
) rect_size rect_size - 2 rect_ofset
rect_ofset - 1 Thread.currentThread
().sleep(100) catch
(InterruptedException exp1)
System.out.println("OOOOPS "
exp1.toString())
Causes shape to disappear when thread reaches
end of cycle
20 panel_lock.grab()
point.x x point.y y try
grow() while (true) while (/
point going left /) Thread.currentThread
().sleep(25) // now move point left a
bit repaint() shrink() panel_l
ock.release() Thread.currentThread().sleep(125
) panel_lock.grab() grow() while (/
point going right /) Thread.currentThrea
d().sleep(25) // now move point right a
bit repaint() catch
(InterruptedException exp1)
System.out.println("OOOOPS "
exp1.toString())
Summary of run( ) methodfor DrawRect class
21run( ) method conceptual statemachine