Results 1 to 6 of 6

Thread: Threads

  1. #1
    dylan is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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.

  3. #3
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default 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.

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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. ;)
    I like likes!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,017
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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?
    I like likes!

Similar Threads

  1. Threads
    By Army in forum New To Java
    Replies: 7
    Last Post: 05-01-2012, 04:52 PM
  2. New to threads
    By armyson in forum Threads and Synchronization
    Replies: 0
    Last Post: 12-09-2011, 07:52 AM
  3. Threads per Connection or Threads per Request
    By Felic in forum New To Java
    Replies: 4
    Last Post: 11-22-2011, 09:15 PM
  4. threads
    By brindha1688 in forum New To Java
    Replies: 3
    Last Post: 05-24-2011, 08:07 PM
  5. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •