Object-Oriented Software Engineering - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Object-Oriented Software Engineering

Description:

Object-Oriented Software Engineering Synchronizing Threads – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 22
Provided by: computing128
Category:

less

Transcript and Presenter's Notes

Title: Object-Oriented Software Engineering


1
Object-Oriented Software Engineering
  • Synchronizing
  • Threads

2
Contents
  • CoordinatesDemo Overview
  • Class Diagram
  • Race in asynchronous version
  • Synchronization with Lock class
  • Wait and NotifyAll methods
  • Conceptual statemachines
  • Get/release cycle
  • Thread run statemachine

3
CoordinatesDemo 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
4
Inner Classes
Can access all methods of outer class
Gives Synchronization between threads
5
Class 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)
7
Race
coordinateArea CoordinateArea
pointPoint
8
Synchronization
coordinateArea CoordinateArea
pointPoint
9
Lock Class
lock open
lock closed
(see lecture 1 for state machines)
10
Lock 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
11
synchronized methods
public synchronized void grab() while
(available false) try wait()
catch (InterruptedException e)
available false
notifyAll()
12
wait( ) method
public synchronized void grab() while
(available false) try wait()
catch (InterruptedException e)
available false
notifyAll()
13
notifyAll( ) method
public synchronized void grab() while
(available false) try wait()
catch (InterruptedException e)
available false
notifyAll()
14
grab() 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
15
release() 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
16
Sequence Diagram for get/release cycle
thread 2
available true
available false
wait( )
thread becomes inactive
available true
notifyAll( )
return from wait
return from grab
17
DrawRect Class
DrawRect public int x public int y private void
grow () private void shrink () public void run()
18
grow( ) 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
19
shrink( ) 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
21
run( ) method conceptual statemachine
Write a Comment
User Comments (0)
About PowerShow.com