Download - Java Synch
-
7/30/2019 Java Synch
1/22
Java Synchronization
Java Synchronization Java uses the concept of monitors
Java uses the concept that every object has a lock
-
7/30/2019 Java Synch
2/22
synchronized Statement
Every object has a lock associated with it.
Calling a synchronized method attempts to possess the lock If no one owns the lock, then this thread has/owns the lock.
If a calling thread does not own the lock (another thread alreadyowns it), the calling thread is placed in the entry set for theobjects lock.
The lock is released when a thread exits the synchronizedmethod.
-
7/30/2019 Java Synch
3/22
Entry Set
-
7/30/2019 Java Synch
4/22
synchronized enter() Method
public synchronized void enter(Object item) {
while (count == BUFFER_SIZE)
Thread.yield();
++count;buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
}
-
7/30/2019 Java Synch
5/22
synchronized remove() Method
public synchronized Object remove() {
Object item;
while (count == 0)
Thread.yield();--count;
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
return item;
}
-
7/30/2019 Java Synch
6/22
The wait() Method
When a thread calls wait(), the following occurs:
- the thread releases the object lock.
- thread state is set to blocked.
- thread is placed in the wait set.
-
7/30/2019 Java Synch
7/22
Entry and Wait Sets
-
7/30/2019 Java Synch
8/22
The notify() Method
When a thread calls notify(), the following occurs:
- selects an arbitrary thread Tfrom the wait set.
- moves Tto the entry set.
- sets Tto Runnable.
Tcan now compete for the objects lock again.
-
7/30/2019 Java Synch
9/22
enter() with wait/notify Methods
public synchronized void enter(Object item) {
while (count == BUFFER_SIZE)
try {
wait();
}
catch (InterruptedException e) { }
}
++count;
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
notify();
}
-
7/30/2019 Java Synch
10/22
remove() with wait/notify Methods
public synchronized Object remove() {
Object item;
while (count == 0)
try {
wait();
}
catch (InterruptedException e) { }
--count;
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
notify();
return item;
}
-
7/30/2019 Java Synch
11/22
Multiple Notifications
notify() selects an arbitrary thread from the wait set. *This maynot be the thread that you want to be selected.
Java does not allow you to specify the thread to be selected.
notifyAll() removes ALL threads from the wait set and placesthem in the entry set. This allows the threads to decide amongthemselves who should proceed next.
notifyAll() is a conservative strategy that works best whenmultiple threads may be in the wait set.
-
7/30/2019 Java Synch
12/22
Reader Methods with JavaSynchronization
public class Database {
public Database() {
readerCount = 0;
dbReading = false;
dbWriting = false;
}public synchronized int startRead() { /* see next slides */ }
public synchronized int endRead() { /* see next slides */ }
public synchronized void startWrite() { /* see next slides */ }
public synchronized void endWrite() { /* see next slides */ }
private int readerCount;
private boolean dbReading;
private boolean dbWriting;
}
-
7/30/2019 Java Synch
13/22
startRead() Method
public synchronized int startRead() {
while (dbWriting == true) {
try {
wait();
}
catch (InterruptedException e) { }
}
++readerCount;
if (readerCount == 1)
dbReading = true;
return readerCount;
}
-
7/30/2019 Java Synch
14/22
endRead() Method
public synchronized int endRead() {
--readerCount
if (readerCount == 0)
db.notifyAll();return readerCount;
}
-
7/30/2019 Java Synch
15/22
Writer Methods
public void startWrite() {
while (dbReading == true || dbWriting == true)
try {
wait();
}
catch (InterruptedException e) { }
dbWriting = true;
}
public void endWrite() {
dbWriting = false;
notifyAll();
}
-
7/30/2019 Java Synch
16/22
Block Synchronization
Blocks of code rather than entire methods may be declaredas synchronized.
This yields a lock scope that is typically smaller than asynchronized method.
-
7/30/2019 Java Synch
17/22
Block Synchronization (cont)
Object mutexLock = new Object();
. . .
public void someMethod() {
// non-critical sectionsynchronized(mutexLock) {
// critical section
}
// non-critical section}
-
7/30/2019 Java Synch
18/22
2 types of monitors
Signal-and-continue Monitor Monitor that allows a thread to signal that the monitor is
available, but does NOT require the thread (signaler) torelease the lock until it exits the monitor, at which point asignaled thread may enter the monitor. (used by Java)
Signal-and-exit Monitor Monitor that requires a thread to release the lock on the
monitor as soon as the thread signals another thread.(signaler immediately leaves, then the signaled threadcomes in.)
-
7/30/2019 Java Synch
19/22
Java Semaphores
Java does not provide a semaphore, but a basic semaphore canbe constructed using Java synchronization mechanism.
-
7/30/2019 Java Synch
20/22
Semaphore Class
public class Semaphore {
public Semaphore() {
value = 0;
}
public Semaphore(int v) {
value = v;
}
public synchronized void P() { /* see next slide */ }
public synchronized void V() { /* see next slide */ }
private int value;
}
-
7/30/2019 Java Synch
21/22
P() Operation
public synchronized void P() {
while (value
-
7/30/2019 Java Synch
22/22
V() Operation
public synchronized void V() {
++value;
notify();}