Download Thread Programming and Synchronized Methods - Slides | SOCB 160 and more Assignments Introduction to Sociology in PDF only on Docsity!
CSE 160 Chien, Spring 2005 Lecture #3, Slide 1
Thread Programming
- Last Time » Threads and Parallelism in Java » Thread class » Runnable Interface - When to use each
- Today » Thread Synchronization - Synchronized Methods - Synchronized Code
- Reminders/Announcements » Homework #1 Due Thursday (in lecture) » TA Office Hours Weds rescheduled => Thursday 10am, APM 4438 » Course Computer Accts (let me know if you don’t yet have your password!)
Threads and Parallelism
- Threads = Independent Loci of Control
Basis of Parallelism
- When no shared objects, no need to coordinate
- So, avoid object sharing where possible.
- Sometimes, sharing is necessary for teamwork, and
achieved a shared goal.
CSE 160 Chien, Spring 2005 Lecture #3, Slide 3
Sharing Objects
- Concurrency =>Two or More Threads Accessing an Object
- If uncontrolled, several things may go wrong » Access in the wrong order » Access in a non-sensical order
- What orders can occur?
public method increase(int value){ A = A + value; B = B + value; return B;}
Read A Add A, value Store A Read B Add B, value Store B
Read A Add A, value Store A Read B Add B, value Store B
Locks
- Locks are Tokens used to control Access » “Take the Lock” » “Have the Lock” » “Release the Lock”
- A Lock ensures exclusive Access (by all locking methods) » No other accesses to object while Lock is held
- So, who holds Locks?
CSE 160 Chien, Spring 2005 Lecture #3, Slide 7
synchronized blocks
- synchronized methods require the entire of scope of the procedure to be governed by the mutual exclusion condition obtained by acquiring the object’s lock public void consume () { synchronized (this) { // code for consuming } }
- Java includes a direct primitive to do this for other blocks of code and objects. an_object is locked when code section is entered
synchronized (an_object){ // … synchronized code goes here … }
Recursion in Methods
- What about recursive locking of an object?
public class Foo {
public void synchronized f() { ... }
public void synchronized g() { ...; f(); ... }
public void synchronized recurse() { …; if (…) recurse(); …}
- When g() is called, it calls f(). What happens?
- recurse() calls itself. What happens?
CSE 160 Chien, Spring 2005 Lecture #3, Slide 9
Recursion and synchronized
blocks
- Recursion can also occur in synchronized blocks.
Foo f = new Foo; synchronized(f) { ...; synchronized (f) { ... } ... }
- Or any combination of synchronized methods or synchronized blocks
- Threads hold locks! » If a thread already has an object’s lock it can simply enter synchronized methods or blocks locking that object » The lock is released when the scope where the lock was actually acquired exits. » >> so recursive calls within a thread don’t deadlock
- Tricky case… what if the references are not textually the same? (variables just point to the same object)
Complex Coordination
- Synchronized manages interaction
- Coordination often requires Teamwork
» wait(), notify(), notifyall()
- wait() method implicitly releases the object’s lock,
then the thread on an internal queue associated with
each object.
- notify() and notifyall() signal waiting thread(s) to tell them to
wakeup
» One or all » After wakeup, they compete to re-acquire the lock on the object they had released
CSE 160 Chien, Spring 2005 Lecture #3, Slide 13
A Shared Queue Example
class SharedQueue { private Element head, tail;
public boolean empty() { return tail == null; }
public synchronized Element remove() { try { while (empty()) wait (); } // wait for an element in the queue catch (InterruptedException e) { return null; } Element p = head; head = head.next; if (head == null) tail = null; return p; }
public synchronized void insert(Element p) if (tail == null) head = p; else tail.next = p; p.next = null; tail = p; notify (); // let one waiter know something is in the queue } }
Comments
- Example of classic “Producer-Consumer” shared
resource.
- Note that wait(), wait(timeout), notify(), and notifyAll()
methods can only be called from a synchronized
method, or a method called by a synchronized
method. i.e., the object must be in the locked state.
CSE 160 Chien, Spring 2005 Lecture #3, Slide 15
ParticleApplet Example
- ParticleApplet creates n Particle objects, sets each particle in autonomous ‘continuous’ motion, and periodically updates the display to show their current positions: » each particle runs in its own Java thread which computes the position of the particle » an additional thread periodically checks the positions of the particles and draws them on the screen
- One thread per particle object,,, lots of parallelism
ParticleApplet
- There are three classes:
- Particle : represents the position and behaviour of a particle and
can draw the particle at its current position;
- ParticleCanvas : provides a drawing area for the Particles, and
periodically asks the Particles to draw themselves; an
- ParticleApplet : creates the Particles and the canvas and sets
the Particles in motion.