Skip to content

Instantly share code, notes, and snippets.

@MauroMombelli
Created October 10, 2014 13:12
Show Gist options
  • Save MauroMombelli/d7dd9cf2f59d6c49e5be to your computer and use it in GitHub Desktop.
Save MauroMombelli/d7dd9cf2f59d6c49e5be to your computer and use it in GitHub Desktop.
java event write read to sinc
private HashMap<CacheKey, AtomicBoolean> waitNotify = new HashMap<>();
@Override
public void objectReaded(Object readObject, SuperSocket superSocket) {
if (readObject instanceof VersionedObject) {
VersionedObject vb = (VersionedObject) readObject;
synchronized (waitNotify) {
AtomicBoolean boolean1 = waitNotify.get(vb.getKey());
synchronized (boolean1) {
if (boolean1 != null) {
boolean1.set(true);
boolean1.notify();
}
}
}
}
listener.objectReaded(readObject, superSocket);
}
public void writeNotify(CacheKey key) {
synchronized (waitNotify) {
waitNotify.put(key, new AtomicBoolean(false));
write(key);
}
}
public boolean waitNotify(CacheKey key, long timeout) {
AtomicBoolean boolean1;
synchronized (waitNotify) {
boolean1 = waitNotify.get(key);
}
if (boolean1 == null) {
return false;
}
synchronized (boolean1) {
if (boolean1.get()) { // se arrivato rot
return true;
}
try {
boolean1.wait(timeout);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return boolean1.get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment