rx java - RxJava Solution to prevent blocking in multithread environment -
the task:
i have set of unique values - ["a","b","c","d"]
multiple threads trying remove values it. when becomes empty code should notify listeners.
in java need block threads each time remove element , check if collection empty prevent sending 2 same events @ time.
so can implement somehow rxjava without blocking execution?
p.s. in real program there many of sets.
your question bit vague sounds you'd want this:
final class rxset { final set<string> elements = new hashset<string>(); final asyncsubject<object> onempty = asyncsubject.create(); public rxset(string... initialvalues) { elements.addall(arrays.aslist(initialvalues)); } public observable<object> onempty() { return onempty; } public boolean take(string item) { boolean wasempty = false; boolean isempty = false; boolean success = false; synchronized (this) { wasempty = set.isempty(); success = set.remove(item); isempty = set.isempty(); } if (!wasempty && isempty) { onempty.onnext("became empty"); onempty.oncomplete(); } return success; } }
Comments
Post a Comment