com.firefly.utils.concurrent.SynchronousObject Maven / Gradle / Ivy
package com.firefly.utils.concurrent;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import com.firefly.utils.log.Log;
import com.firefly.utils.log.LogFactory;
public class SynchronousObject {
private static Log log = LogFactory.getInstance().getLog("firefly-system");
private SynchronousQueue queue = new SynchronousQueue();
public void put(T obj, long timeout) {
try {
queue.offer(obj, timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
log.error("put synchronous obj error", e);
}
}
public T get(long timeout) {
T t = null;
try {
t = queue.poll(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
log.error("get synchronous obj error", e);
}
return t;
}
}