java - Does DeferredResult have a race condition when returning a potentially already-set instance? -
i'm using deferredresult in spring mvc application handle server-side processing of potentially long-running action. might very fast, or take second or two.
but in either case, incoming http request causes action pushed queue, separate thread (via executorservice
) responsible consuming. callback called, notifying pusher operation has completed.
i refactored of behavior utility method:
public static deferredresult<string> toresponse(gamemanager gamemanager, final player player, action action) { deferredresult<string> deferredresult = new deferredresult<>(); gamemanager.execute(action, new handler<result>() { @override public void handle(result result) { jsonobject obj; try { obj = gamemanager.getgamejson(player); obj.put("success", result.getresult()); obj.put("message", result.getmessage()); deferredresult.setresult(obj.tostring()); // point b } catch (jsonexception e) { deferredresult.seterrorresult(e); } } }); return deferredresult; // point }
but i'm wondering what happens if execution of action happens setresult()
method called (point b
) on deferredresult
before has been returned (point a
) calling method.
will spring see returned deferredresult
has value , handle it, or begin "watching" setter called after instance has been provided?
i've not used spring class deferredresult<>
pretty poor implementation of deferred if settlement timing made difference downstream behaviour.
it seems safe assume behaviour identical regardless of asynchronous process' timing - milliseconds, seconds or whatever, proviso timeout didn't occur in case ontimeout
handler run (if set). if deferred settled synchronously, in same code block created it, caller function should act on outcome expected.
if assumption not valid class deferredresult<>
not fit purpose , shouldn't used.
Comments
Post a Comment