title: Simple Java code allow single thread access and other threads to skip/move on
body: <p>In others words I don't want a thread to wait if it cannot access the lock (as in synchonization), I want to thread execution to simply return immediately at that point if it cannot obtain the lock.</p>
  
  <p>As simple boolean lock like this would potentially allow more than one thread to access.</p>
  
  <pre><code>private static boolean lockAvailable = true;
  
  private boolean acquireLock() {
    if(lockAvailable) {
      lockAvailable = false;
      return true;
    }
    return false;
  }
  </code></pre>
  
  <p>Am I missing something? What is the best/simplest way to achieve this?</p>
  
  <p>Edit:</p>
  
  <p>Thanks for pointing out Semaphore (!)</p>
  
  <p>So looking at it again this code is bullet proof?</p>
  
  <pre><code>private final static Semaphore lock = new Semaphore(1, true);   
  
  public void tryAndDoSomething() {
    if(lock.tryAcquire()) {
      try {
        // only single thread can access here at one time
      } finally {
        lock.release();
      }
    }
  }
  </code></pre>
  
  <p>Update:</p>
  
  <p>I realised I needed reentrant capability so I created a simple non blocking reentrant. Posting the code for anyone that is interested in how you might do this. Anyone who wants this type of functionality should of course use the <em>existing</em> Java Class java.util.concurrent.locks.ReentrantLock :|</p>
  
  <pre><code>import java.util.concurrent.atomic.AtomicBoolean;
  import java.util.concurrent.atomic.AtomicInteger;
  
  /**
   * SimpleNonBlockingLock ensures that only a single thread can call protected code at any one time,
   * while allowing other threads to by pass the protected code if the lock is unavailable.
   * The thread owning the lock can access any code protected by the lock (the lock is 'reentrant').
   * To function correctly the protected code must be executed in a try/finally blocks. The
   * finally block must call the tryRelease. Example code:
   * 
   *  private final SimpleNonBlockingLock lock = new SimpleNonBlockingLock();
   * 
   *  if(lock.tryAcquire()) {
   *      try {
   *              // access protected code
   *      } finally {
   *          lock.tryRelease();
   *      }
   *  }
   *
   * This code is for demonstration only and should not be used. I have tested it and it 'seems to' work.
   * However it may contain horrific bugs!
   * 
   * The Java class java.util.concurrent.locks.ReentrantLock has been around since Java 5.0 and contains all (and more) 
   * of this functionality. Its also been thoroughly tested!
   */
  public class SimpleNonBlockingLock {
  
      // Atomic locking mechanism
      private final AtomicBoolean locked = new AtomicBoolean();
  
      // Atomic integer containing the next thread ID to be assigned
      private static final AtomicInteger nextId = new AtomicInteger(0);
  
      // Unique ID of thread which currently has lock
      private int threadUniqueId = -1;
  
      // Tracks number of tryAcquire calls made by thread with lock
      private int lockCount = 0;
  
      // Thread local variable containing each thread's ID
      private static final ThreadLocal&lt;Integer&gt; threadId = new ThreadLocal&lt;Integer&gt;() {
              @Override protected Integer initialValue() {
                  return nextId.getAndIncrement();
          }
      };
  
      public synchronized boolean tryAcquire() {      
          // Allow owning thread to acquire
          if(threadUniqueId == getCurrentThreadUniqueId()) {
              lockCount++;
              return true;
          }       
          // If locked then do not allow
          if (locked.get()) {return false;}           
          // Attempt to acquire lock      
          boolean attemptAcquire = locked.compareAndSet(false, true);     
          // If successful then set threadUniqueId for the thread, and increment lock count
          if(attemptAcquire) {
              threadUniqueId = getCurrentThreadUniqueId();
              lockCount++;
          }       
          // Return result of attempt to acquire lock
          return attemptAcquire;
      }
  
      public synchronized boolean tryRelease() {
          if (!locked.get()) {
              // Lock is currently available - no need to try and release
              return true;
          } else {
              // Decrement the lock count
              lockCount--;
              // If lock count is zero we release lock, and reset the threadUniqueId
              if(lockCount == 0) {
                  threadUniqueId = -1;
                  return locked.compareAndSet(true, false);
              } 
              return false;
          }   
      }
  
      // Returns the current thread's unique ID, assigning it if necessary
      public static int getCurrentThreadUniqueId() {      
          return threadId.get();
      }   
  }
  </code></pre>