multithreading - Java concurrency - is monitor blocked? -
is there way tell 1 thread (say 1 locks monitor object) if thread blocked / waiting on same monitor?
example scenario -
a "collector" thread reads data shared object, while "updater" thread might blocked , waiting collection end. collector know when finishes collecting, possible data update pending yield collected data might invalid.
in case collecting might time consuming operation, , in next stage "collector" thread analyzes data more time, might redundant operation in many cases data invalid.
is there way tell 1 thread (say 1 locks monitor object) if thread block/waiting on same monitor?
no, not object itself. can, @evgeniy mentioned, use other of java.util.concurrent.locks.*
classes allow view queued members, not synchronized (lock)
type of object monitor.
i collector know when finishes collecting, possible data update pending yield collected data might invalid.
how having blockingqueue
of updates collector check queue , see if non-empty. updater thread(s) adding update
information blockingqueue
, collector dequeue updates , make adjustments. check length of queue , make decision whether or not needed move analysis mode.
private blockingqueue<update> updatequeue = new linkedblockingqueue<update>(); ... // called updater thread(s) public void updatedata(update update) { updatequeue.put(update); } // called collector public void collect() { while (!thread.currentthread().isinterrupted()) { update update = updatequeue.take(); updatevalues(update); if (updatequeue.isempty()) { analyzedata(); } } }
regardless of how it, going need account new data update using other mechanism opposed checking threads' blocked state.
Comments
Post a Comment