Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 269
Ľubomír Avatar asked Sep 11 '16 15:09

Ľubomír


People also ask

Which thread has highest priority in Java?

JVM selects to run a Runnable thread with the highest priority. All Java threads have a priority in the range 1-10. priority ie. priority by default is 5.

How do you set a priority in Java?

It can be changed using the method setPriority() of class Thread. There are three static variables for thread priority in Java i.e. MIN_PRIORITY, MAX_PRIORITY and NORM_PRIORITY. The values of these variables are 1, 10 and 5 respectively.

Does Java support thread priority?

The Java runtime supports a very simple, deterministic scheduling algorithm known as fixed priority scheduling. This algorithm schedules threads based on their priority relative to other runnable threads. When a Java thread is created, it inherits its priority from the thread that created it.


2 Answers

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
like image 169
jtahlborn Avatar answered Oct 16 '22 08:10

jtahlborn


There're no synchronization primitives in Java that would allow you to prioritise one thread over others in the manner you want.

But you could use another approach to solving your problem. Instead of synchronizing threads, make them produce small tasks (for instance, Runnable objects) and put those tasks into a PriorityBlockingQueue with tasks from the GUI thread having the highest priority. A single working thread will poll tasks from this queue and execute them. That would guarantee both mutual exclusion and prioritization.

There're special constructors in ThreadPoolExecutor that accept blocking queues. So, all you need is such an executor with a single thread provided with your PriorityBlockingQueue<Runnable>. Then submit your tasks to this executor and it will take care of the rest.

Should you decide to choose this approach, this post might be of interest to you: How to implement PriorityBlockingQueue with ThreadPoolExecutor and custom tasks

like image 37
Andrew Lygin Avatar answered Oct 16 '22 09:10

Andrew Lygin