spring - Connecting multiple flex clients to a single java class -
i have multi-user application consisting of flex client , blazeds/spring/java backend - have main elements working fine ie. sending messages destination, consuming , producing. flex clients able send , retrieve string class no problem. want have 2 clients access same variable..in crude sample i'm sending guid each swf append string _players server side. happens when launch swf a, recieves guid fine, swf b. swf recieves guid swf b, swf b not recieve swf a. btw same swf code launched twice each in different browser.
can see i'm going wrong or might better solution?
public class gamefeed { private static ganefeedthread thread; private final messagetemplate template; public gamefeed(messagetemplate template) { this.template = template; } public void start() { if (thread == null) { thread = new ganefeedthread(this.template); thread.start(); } } public void stop() { thread.running = false; thread = null; } public static class ganefeedthread extends thread { public boolean running = false; private final messagetemplate template; public ganefeedthread(messagetemplate template) { this.template = template; } private static string _players; public void addplayer(string name) { _players += name + ","; } while (this.running) { this.template.send("game-feed", _players); }
you have threading problem in class. not sure if cause of problem - could.
it seams sharing data though _player
variable. variable not thread safe. has 2 major problem:
- issue 1 : if 2 clients invoke addplayer method @ same time - not clear happen player variable - alt least have lost update
- issue 2: (this maybe cause) - java memory model not guarantee _player variable updated in both threads without proper concurrency management.
to fix have 2 things:
- first: wrap
_players += name + ",";
in synchronized block (for issue 1) - second: mark
_players
volatile
(for issue 2)
@see http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html
Comments
Post a Comment