[java] Using synchronized

Started by
9 comments, last by pamaro 21 years, 4 months ago
Just a final post to mention the solution I ended up using (it might be useful for someone else having the same problem).

I sort of followed Henry''s static suggestion with a small little twist. The Worker class (which was the "thread" class) contained a static synchronized method called "file_access_control". I did something quite simple: after checking the command line and noticing which was the command given by the user, I would use that function to call the appropriate method to take care of that command ("send_message", "read_message", etc). This way, I made sure that it was impossible for two different threads to access the same file (for example, if one user was deleting a message and another user was, at the same time, sending him a message). It''s pretty much a basic "semaphore" implementation, but it worked nice .

Thanks for the input!

Pedro Amaro
Play Java games at http://pedroamaro.pt.vu
---------------------------------------------Looking for an artist and composer for a web and mobile game

This topic is closed to new replies.

Advertisement