I've been building a simple rock, paper, scissors game that's played between two server clients for a college lab. I have a very simple server that starts the game when two clients have signed on. It works like this:
The server creates a game object and waits for player clients to ask for a game.
Once two players request a game the server creates a player 1 object and a player 2 object, passing the game object into their constructors.
The players call methods on the player objects which in turn call methods on game object. The game object has synchronized methods in it.
Each player makes their move, which is stored in the game object, then the game object calculates the winner.
The player objects call the same methods in the game object, this all works fine. But a friend of mine was puzzled by the fact I don't call wait() or notify() in any of my methods. He has already presented his lab, and passed. He asked about wait() and notify() within a synchronized method but he said her response was vague and he still isn't sure, but I should probably put them in just in case. So what's the deal?? Do I need wait() and notify() in a synchronized method?
Also, I read that synchronized doesn't work between Java virtual machines, is this true? If so, how does this affect my game, is synchronized doing anything at all??
Many thanks.
wait()/notifyAll()pair if the only goal is the mutual exclusion, they needed if two or more threads need to co-operate their actions on some data structure, like set of tasks. And none of JVM synchronization primitives works across several JVMs. But, usually, if your code used by several clients over network, you'll get several client-serving threads in your server application, thus, you'd need co-operation between these threads, thus,wait()/notifyAll(), etc. It'd be useful to familiriaze yourself with Oracle threads tutorial (google for it).