com.lti.utils.synchronization.SynchronizedObjectHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fmj Show documentation
Show all versions of fmj Show documentation
Freedom for Media in Java
package com.lti.utils.synchronization;
/**
*
* @author Ken Larson
*
*/
public class SynchronizedObjectHolder
{
private T object;
public SynchronizedObjectHolder()
{
super();
}
public SynchronizedObjectHolder(T value)
{
setObject(value);
}
public synchronized T getObject()
{
return object;
}
public synchronized void setObject(T value)
{
object = value;
notifyAll();
}
public synchronized void waitUntilNotNull() throws InterruptedException
{
while (object == null)
{
wait();
}
}
/**
* @return true if value is now non-null, false if timeout occurred
*/
public synchronized boolean waitUntilNotNull(int timeout)
throws InterruptedException
{
long start = System.currentTimeMillis();
while (object == null)
{
long now = System.currentTimeMillis();
long diff = now - start;
long wait = timeout - diff;
if (wait <= 0)
return false;
wait(wait);
}
return true;
}
}