Sunday, August 28, 2016

Locks in java


  • The java.util.concurrent.locks package provides support for locks, which are objects that offer an alternative to using synchronized to control access to a shared resource
  • Before accessing a shared resource, the lock that protects that resource is acquired. When access to the resource is complete, the lock is released
  •  If a second thread attempts to acquire the lock when it is in use by another thread, the second thread will suspend until the lock is released.
  • All locks implement the Lock interface.
  • To acquire a lock, call lock( ). If the lock is unavailable, lock( ) will wait.
  •  To release a lock, call unlock( ). 
  • To see if a lock is available, and to acquire it if it is, call tryLock( ). This method will not wait for the lock if it is unavailable. Instead, it returns true if the lock is acquired and false otherwise. 
  • The newCondition( ) method returns a Condition object associated with the lock. Using a Condition, you gain detailed control of the lock through methods such as await( ) and signal( ), which provide functionality similar to Object.wait( ) and Object.notify( ).

No comments:

Post a Comment

Required details

--------------------------------------------------------------------------------------------------------------------------- C:\Program File...