Title: Distributed Software Engineering
1Looking for a better thread synchronization and
cooperation mechanism
2Limitations of built-in synchronization
- There is no way
- To back off from an attempt to acquire a lock if
already held - To give up after waiting for an specific time
- To cancel a lock attempt after an interrupt
- There is no way to alter semantics of lock w.r.t
- Reentrancy
- Read vs. write protection
- Fairness
3Limitations of built-in synchronization
- There is no access control for synchronization.
Any method can perform synchronized(obj) for any
accessible object, leading to potential
denial-of-service caused by the lock. - Synchronization within methods and blocks limits
its use to strict block-structure locking. - Cannot acquire a lock in one method and release
in another. - In particular, cannot place a lock to be active
across several methods
4User-defined classes to control locking
- Problems mentioned can be overcome using
user-defined classes to control locking - But solutions provided come at a price
- More awkward coding idioms
- Less automatic enforcement of correct usage
- More care and discipline in coding
- Greater overhead since are less optimized than
built-in mechanisms - Their use rely in following strict coding
standards and practices.
5/ A class designed to aliviate the problems
with Java's built-in synchronization.
/ public class BusyFlag protected Thread
busyFlag null protected int busyCount 0
/ Secures Busy flag for
Thread.currentThread(). Increments number
of busyFlag() held by the thread.
/ public void getBusyFlag() while
(tryGetBusyFlag() false) try
Thread.sleep(100) catch (Exception e)
6/ Attempting to obtain the BusyFlag may
fail if already some other thread owns the
flag. ensure If
getBusyFlagOwner() null, then
getBusyFlagOwner() Thread.currentThread()
and returns true else returns false.
/ public synchronized boolean tryGetBusyFlag()
if (busyFlag null) busyFlag
Thread.currentThread() busyCount
1 return true if (busyFlag
Thread.currentThread()) busyCount retur
n true return false
7 / Current thread busyCount() will be
decremented if getBusyFlagOwner()
Thread.currentThread() In this case if the
count reaches 0, BusyFlag instance is
detached from the thread that owned it.
ensure If (getBusyFlagOwner()
Thread.currentThread()) busyCount()
old.busyCount() - 1 if busyCount() 0
getBusyFlagOwner() null. / public
synchronized void freeBusyFlag () if
(getBusyFlagOwner() Thread.currentThread())
busyCount-- if (busyCount
0) busyFlag null public
synchronized Thread getBusyFlagOwner() return
busyFlag
8/ returns number of BusyFlag held by
current thread. / public synchronized
int busyCount() if (getBusyFlagOwner()
Thread.currentThread()) return
busyCount else return 0 // end of
BusyFlag
9How to use BusyFlag
- Given an instance of an object a (of class A)
used in some other class instance b, (of class B)
and for which b needs atomic access to any of
as methods - Provide a method in class B, where it acquires
as lock. - Provide a method in class B, where it releases
as lock. - Class A will have an instance variable of
BusyFlag. - Provide methods in class A to lock and unlock an
instance. - Class A will have the appropriate methods
synchronized, if need be. A user will choose
between properly using User-defined locks or the
given synchronized code.