Example #1
0
 /*
  * returns true if this thread can obtain the global lock or already has the lock;
  * otherwise this loader and thread are added to the waitingList
  */
 private static synchronized boolean tryLock(Thread currentThread, Object loader) {
   if (lockThread == currentThread) {
     lockCount++;
     return true;
   }
   if (lockThread == null) {
     lockCount++;
     lockThread = currentThread;
     return true;
   }
   waitingList.add(new Object[] {currentThread, loader});
   return false;
 }
Example #2
0
  /*
   * unlocks the global lock and notifies the first waiting thread that they
   * now have the lock
   */
  private static void unlock() {
    Thread waitingThread = null;
    Object loader = null;
    synchronized (BundleLoader.class) {
      lockCount--;
      if (lockCount != 0) return;

      if (waitingList.isEmpty()) {
        lockThread = null;
        return;
      }

      Object[] waiting = (Object[]) waitingList.get(0);
      waitingThread = (Thread) waiting[0];
      loader = waiting[1];
    }
    synchronized (loader) {
      synchronized (BundleLoader.class) {
        lockThread = waitingThread;
        waitingList.remove(0);
        loader.notifyAll();
      }
    }
  }