Montag, 9. Februar 2009

EE4216/EE5816 java locks

EE4216/EE5816
Internet Client Server Computing

java locks

notes on Liang(2009), page 975

1. A lock is an instance of the Lock interface. It has methods for acquiring and releasing locks.
They are the lock() and unlock() methods.

2. A newCondition() method is used to create a Condition object. A Condtion object is used for thread communications.

3. Lock is an interface. ReentrantLock is a concrete implementation of Lock for creating mutually exclusive locks.

For example:

// create a lock
private Lock lock = new
ReentrantLock();
. . .
. . .
// use this pattern !!

lock.lock() //acquire a lock
try {
//access the resources
//protected by this lock . . .
}
catch (InterruptedException e) {
}
finally {
lock.unlock();
// release the lock
}

notes:
http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/package-summary.html



Keine Kommentare: