1

Suppose a field in an object that changes from null to non-null and back and forth, etc., depending on the operation of one thread.

A second thread shall lazily take some action whenever it happens to get hold of a non-null value. In particular the second thread shall wait until the value switches to non-null. If it falls out of the wait, I want to be sure that it has a non-null in its hand.

This does not seem like a queue situation, because the second thread will not take the element away, it just uses it if one happens to be available.

It also does not fit for semaphore use, because again it would not .acquire() a permit.

Rather it reminds of of compare-and-get with built in wait, but this does not seem to exist.

Is there a predefined device for this in java.util.concurrent that I miss to identify. How can this be done?

This is similar but does not have an accepted answer or one that would help here.

4
  • Your approach has a fundamental flaw - you can't guarantee that the lazy thread will wake up between field changes, so it might not catch all events.
    – Malt
    Commented Sep 11, 2016 at 16:34
  • That's not a fundamental flaw but intended behavior. That's why I write: "if it happens to get hold of a null-value". If it does not, this is not important.
    – Harald
    Commented Sep 11, 2016 at 16:39
  • Will multiple threads be setting the value?
    – erickson
    Commented Sep 11, 2016 at 17:00
  • Ah good question. Just one thread will be changing with the value.
    – Harald
    Commented Sep 11, 2016 at 17:03

1 Answer 1

2

Here's an implementation relying on ReentrantLock to manage a volatile field. This borrows heavily from the double-checked locking idiom, but instead of creating the value itself, a read operation waits on a condition to signal that a value has been set.

The get() method is overloaded with a version that accepts a timeout. Both versions are interruptible.

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class BlockingRef<V> {

  private final Lock lock = new ReentrantLock(true);

  private final Condition signal = lock.newCondition();

  private volatile V value;

  public BlockingRef() {
    this(null);
  }

  public BlockingRef(V initialValue) {
    this.value = initialValue;
  }

  public final void set(V value) {
    lock.lock();
    try {
      this.value = value;
      signal.signalAll();
    } finally {
      lock.unlock();
    }
  }

  public final V get() throws InterruptedException {
    V result = value;
    if (result == null) {
      lock.lockInterruptibly();
      try {
        for (result = value; result == null; result = value)
          signal.await();
      } finally {
        lock.unlock();
      }
    }
    return result;
  }

  public final V get(long time, TimeUnit unit) 
    throws TimeoutException, InterruptedException 
  {
    V result = value;
    if (result == null) {
      long start = System.nanoTime();
      if (!lock.tryLock(time, unit)) throw new TimeoutException();
      try {
        time = unit.toNanos(time);
        for (result = value; result == null; result = value) {
          long wait = time - (System.nanoTime() - start);
          if (wait <= 0) throw new TimeoutException();
          signal.await(wait, TimeUnit.NANOSECONDS);
        }
      } finally {
        lock.unlock();
      }
    }
    return result;
  }

  @Override
  public String toString() {
    return String.valueOf(value);
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.