-
Threads
I started this thread to talk about when it is necessary to use a thread in java. Im making a game in Java right now its a platformer. I was wondering when is it a good idea to use a thread? I have two threads right now a jumping thread for the player and a gravity thread for every character in my game. My programming teacher said "I would never have two seperate threads that are similar in design." Thats not exactly what he said but, I would like some tips or hints on when I should use threads in a program or my game. Please leave anything that could be of help, thank you.
-
Re: Threads
Keep in mind that the main part of your program is also a thread. So, you have three threads. One for the player, one for gravity and the main thread. I am not too sure why you would need a separate thread for the player and gravity, but I do not know the complexity of the game. Generally I use threads when there is a task that needs to be done and not interfere with other tasks. For example, I made a simple GUI program in swing. The program connected to an SQL database and pulled data from it. I originally had all the logic of connecting in the same thread as the GUI. So, whenever I connected to the server, the entire GUI would free because it was waiting for it to connect. I solved it by moving it into its own thread, and it was able to do its thing without interfering with the Swing thread.
-
Re: Threads
I think that multiple threads are warranted when you're doing input/output such as having sockets in a client/server program.
For example, for a ServerSocket, when you try to allow a client socket to connect the thread blocks until a connection is made. Also in client/server, I would have a Thread on the server for each client so that if you start to read from a socket, and the Thread blocks, it doesn't freeze any other clients.
So, if you ever do a game where you have a server that serves a number of clients, you'd want multiple threads.
That's all I have.
-
Re: Threads
I would not use separate threads in a platform game. Threading is complicated and introduces a lot of tricky errors if not done properly.
I would recommend the use of threads for asynchronous operations only, like the ones mentioned above. A common usage of threading is AI for computer moves and background tasks like music, sound management or graphics engines maybe... but this is far beyond a platform game maybe. ;)
-
Re: Threads
You want two (or more) threads if you don't want the execution flow of one thread influences the execution flow of the other thread, e.g. if one thread blocks for input or has a lot of other things to do, you don't want the other thread to stop or slow down. There is a great package in the core distribution: java.util.concurrent but most peope prefer to ignore it (sadly enough); have a look at it. I like the idea of a Player and Gravity being two separate threads if you consider Gravity another type of Player. They both do what the have to do and the Thread model fits fine.
kind regards,
Jos
-
Re: Threads
For a platform game you usually do not want to have gravity independant of input or movement thread - what actually happens then is that when the input thread is slowed the gravity still may run and you will get such effects that your player falls down because physics (movement command to gravity while jumping) are changed due to that... you want a synchonized execution of those vital threads or you may experience annoying effects, right?