Download presentation
Presentation is loading. Please wait.
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 click Square oscillates across grid from side to side. Square is created and controlled by thread class DrawRect. Races are prevented by synchronizing threads. Synchronization with Lock class. Full code available on web site for this lecture
4
Inner Classes CoordinatesDemo Can access all methods of outer class
CoordinateArea Can access all methods of outer class Lock DrawRect Gives Synchronization between threads
5
Class Diagram MouseInputListener CoordinatesDemo JComponent Graphics
JFrame paints with CoordinateArea inner class dependency contained in 1 Thread inner class dependency Black triangles indicate direction to read association 1 synchronizes Lock DrawRect 1 0...*
6
frame: JFrame container (frame's content pane) coordinateArea
7
Race draw_rect(1) :DrawRect coordinateArea: CoordinateArea
point:Point loop par change value repaint( ) change value repaint( )
8
<<creates>>
Synchronization draw_rect(1) :DrawRect coordinateArea: CoordinateArea draw_rect(2) :DrawRect point:Point panel_lock :Lock thread 2 locked out until grab( ) returns <<creates>> grab( ) change value grab( ) repaint( ) release( ) change value repaint( ) release( )
9
Lock Class Lock private boolean available = true
public synchronized void grab( ) public synchronized void release( ) Only one object may call method at a time. Only after method returns may another object call it. grab( ) [available = true] / available = false lock open lock closed release( ) [available = false] / available = true (see lecture 1 for state machines)
10
Lock Java Code methods are almost identical Only difference is duality
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() {
If a thread calls a synchronised method, that method can then control the behaviour of the calling thread. 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(); wait( ) causes the thread that is invoking this method to wait.
13
notifyAll( ) method public synchronized void grab() {
while (available == false) { try { wait(); } catch (InterruptedException e) { } available = false; notifyAll(); notifyAll( ) alerts all threads that have been suspended, waiting to execute one of the synchronized methods. The Java run time environment chooses one of these to execute each available synchronized method.
14
grab() conceptual statemachine
entry state when thread calls method this is initial state exit state after this state the method returns [available == true] / available = false wait notifyAll [available == false]
15
release() conceptual statemachine
entry state when thread calls method this is initial state exit state after this state the method returns [available == false] / available = true wait notifyAll [available == true]
16
Sequence Diagram for get/release cycle
thread 1 Lock thread 2 Java grab( ) {available == true} available = false grab( ) wait( ) notifyAll( ) thread becomes inactive release( ) available = true notifyAll( ) wake up return from wait notifyAll( ) 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 < itr_rect; i++) { Thread.currentThread().sleep(50); rect_size += 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 point set to correct initial value for this thread
19
shrink( ) method Causes shape to private void shrink () {
point.x = x; point.y = y; try { while (rect_size > 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_lock.release(); Thread.currentThread().sleep(125); grow(); while (/* point going right */) { // now move point right a bit } catch (InterruptedException exp1) { System.out.println("OOOOPS: " + exp1.toString()); Summary of run( ) method for DrawRect class
21
run( ) method conceptual statemachine
modify point [point in coordinate area] / repaint( ) [point at edge coordinate area] move right grow( ) got lock panel_lock.grab() take a nap panel_lock.release() initialise panel_lock.grab() regain lock sleep give up lock [point at edge coordinate area] / shrink ( ) move left grow( ) [point in coordinate area] / modify point, repaint( )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.