public class

Lock

extends Object
java.lang.Object
   ↳ sun.misc.Lock
Known Direct Subclasses

Class Overview

The Lock class provides a simple, useful interface to a lock. Unlike monitors which synchronize access to an object, locks synchronize access to an arbitrary set of resources (objects, methods, variables, etc.).

The programmer using locks must be responsible for clearly defining the semantics of their use and should handle deadlock avoidance in the face of exceptions.

For example, if you want to protect a set of method invocations with a lock, and one of the methods may throw an exception, you must be prepared to release the lock similarly to the following example:

      class SomeClass {
          Lock myLock = new Lock();

          void someMethod() {
              myLock.lock();
              try {
                  StartOperation();
                  ContinueOperation();
                  EndOperation();
              } finally {
                  myLock.unlock();
              }
          }
      }
 

Summary

Public Constructors
Lock()
Create a lock, which is initially not locked.
Public Methods
synchronized final void lock()
Acquire the lock.
synchronized final void unlock()
Release the lock.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Lock ()

Create a lock, which is initially not locked.

Public Methods

public final synchronized void lock ()

Acquire the lock. If someone else has the lock, wait until it has been freed, and then try to acquire it again. This method will not return until the lock has been acquired.

Throws
InterruptedException if any thread has interrupted this thread.

public final synchronized void unlock ()

Release the lock. If someone else is waiting for the lock, the will be notitified so they can try to acquire the lock again.