Java Question

Java - Priority in semaphore

I have multiple threads accessing an external resource – a broswer. But only one thread can access it at a time. So, I am using a semaphore to synchronise them. However, one thread, which takes input from the GUI and then access the browser for the results, should have priority over other threads and I am not sure how to use a semaphore to achieve it.

I was thinking that every thread after acquiring the semaphore checks if there is the priority thread waiting in the queue and if yes, then it releases it and waits again. Only the priority thread doesn't release it once it is acquired.

Is this a good solution or is there anything else in Java API I could use?

Answer

Here's a simple, no frills answer. This is similar to how a read/write lock works, except that every locker has exclusive access (normally all readers proceed in parallel). Note that it does not use Semaphore because that is almost always the wrong construct to use.

public class PrioLock {
  private boolean _locked;
  private boolean _priorityWaiting;

  public synchronized void lock() throws InterruptedException {
    while(_locked || _priorityWaiting) {
      wait();
    }
    _locked = true;
  }

  public synchronized void lockPriority() throws InterruptedException {
    _priorityWaiting = true;
    try {
      while(_locked) {
          wait();
      }
      _locked = true;
    } finally {
      _priorityWaiting = false;
    }
  }

  public synchronized void unlock() {
    _locked = false;
    notifyAll();
  }
}

You would use it like one of the Lock types in java.util.concurrent:

Normal threads:

_prioLock.lock();
try {
  // ... use resource here ...
} finally {
  _prioLock.unlock();
}

"Priority" thread:

_prioLock.lockPriority();
try {
  // ... use resource here ...
} finally {
  _prioLock.unlock();
}

UPDATE:

Response to comment regarding "preemptive" thread interactions:

In the general sense, you cannot do that. you could build custom functionality which added "pause points" to the locked section which would allow a low priority thread to yield to a high priority thread, but that would be fraught with peril.

The only thing you could realistically do is interrupt the working thread causing it to exit the locked code block (assuming that your working code responded to interruption). This would allow a high priority thread to proceed quicker at the expense of the low priority thread losing in progress work (and you might have to implement rollback logic as well).

in order to implement this you would need to:

  1. record the "current thread" when locking succeeds.
  2. in lockPriority(), interrupt the "current thread" if found
  3. implement the logic between the lock()/unlock() (low priority) calls so that:
    1. it responds to interruption in a reasonable time-frame
    2. it implements any necessary "rollback" code when interrupted
  4. potentially implement "retry" logic outside the lock()/unlock() (low priority) calls in order to re-do any work lost when interrupted