I am given some skeletons of classes that I must implement (I personally don’t really agree with the design of the program but I am not at mercy to change it 🙁 ) and they work in this manner:
I have a Game class which implements Runnable and represents a chess game.
A Server class will have a list of multiple Game classes that it keeps track of.
Ok this makes sense, Game implements Runnable so the Server can put each game in its own thread.
I am a bit confused at how java’s threads work.
What I know is the following:
After tying my Runnable class to a thread and calling the .start() method, the Runnable class’s run() method is called.
However, I have some other methods inside my Game class such as:
capturePiece()
playerMakesMove()
etc
In the current design, it is up to the Server to handle game actions. When a player wants to capture a piece, the Server will call game.capturePiece().
In this case, is capturePiece() being run on the Game thread, or the Server‘s thread? (the caller’s thread or the callee’s thread)
In this case, what would run() even do?
Any method, in any programming language, executes in the same thread as the caller. When you call
Thread.start()it runs in the same thread that called it.Now, you know that the
run()method of a Thread does not execute in the same thread asstart. But that is because start does not itself call run. You will have to read up more on threads to get the complete picture but just imagine that start only crates a new thread with some data structure (theRunnable) and the newly created thread looks at that data structure, identifies the Runnable and executes itsrunmethod.And that is really the only way control passes from one thread to another: one thread generates some data and other thread picks it up and processes it. Control does not pass from one thread to another, it is inter-thread communication and co-ordination.
If the methods of
Gameare called byServerthen the threads are not going to have anything to do, are they? But instead, if the Server does not call the method directly but instead represents the action as data thenGame.run()can pick the action and perform it, in its own thread.Now the only question is where can
Serverput the data such that eachGame.run(), running in its own thread knows to pick it up from. One option is to use BlockingQueue. The Server can put theseActionobjects in the queue and the Game thread can pick it up. How will the two know to use the same queue? There many different ways, one is for server to create the game with a queue and store a map on its side. As in the skeleton below: