Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: A Multithreading Question

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    My Mood
    Cheerful
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default A Multithreading Question

    Hi guys, a quick theory question.

    When building a game I discovered the benefits of creating a separate thread to handle my path finding AI for all entities.
    However once I had set this up I realised just how beneficial it was, so my question:

    What downsides are there to using lots of threads, such as a separate update thread for every single entity in a game?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: A Multithreading Question

    The threads can become out of sync. Google: Race Condition
    Let's say we have a variable: var
    Now let's say we have two threads: Thread_A, Thread_B
    At a certain point in the program, Thread_A will try to read the value of var, while Thread_B will try to write the value of var.
    Here's a question for you: which will happen first? Will Thread_A read the value of var before Thread_B writes to it, or will Thread_B write the value of var before Thread_A reads it?
    The answer: there is no way to know. One could happen before the other some times, while the other could happen first other times.

    To solve this issue, it is best to always synchronize threads before trying to access shared variables. Read: Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    GregBrannon (April 14th, 2014)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: A Multithreading Question

    What downsides are there to using lots of threads, such as a separate update thread for every single entity in a game?
    Lots of variables involved, but in general creating many threads at once can cause complications. Threads need to be created and managed (overhead), threads can deadlock, code must be synchronized, each thread requires free CPU, etc...these variables each cause resources to get bogged down (a UI could lock up, as can garbage collection or even the system itself). While you don't mention how many threads you are talking about, thread pools are often a better choice - dispatching the work (in a managed way) as necessary to the thread pool without overloading the system.
    See Thread Pools (The Java™ Tutorials > Essential Classes > Concurrency)

  5. The Following 2 Users Say Thank You to copeg For This Useful Post:

    fatchance30 (April 14th, 2014), GregBrannon (April 14th, 2014)

  6. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: A Multithreading Question

    Also, from a performance point of view, it is also important to remember that there really isn't such a thing as "true" multitasking. The number of processes which a computer can run at any given time depends on the number of CPUs it has access to. A CPU can only run a single process at a time. Computers *fake* multitasking by quickly cycling through processes, but ultimately only one of them is running at any given time.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    12
    My Mood
    Cheerful
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: A Multithreading Question

    Thanks guys that makes a lot of sense, one question though: lets say that theoretically we have no clashing variables and no additional complications, Would a program that uses 10 threads use more of the computers resources than a single threaded program of the same function?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A Multithreading Question

    there really isn't such a thing as "true" multitasking
    Not true for computers with more than one "core". Each core can be executing a thread at the same time as the other threads are being executed on other cores.
    Many modern PCs have multiple cores.
    I worked on a multiple core CPU in the mid 1980s.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: A Multithreading Question

    Quote Originally Posted by Norm View Post
    Not true for computers with more than one "core". Each core can be executing a thread at the same time as the other threads are being executed on other cores.
    Many modern PCs have multiple cores.
    I worked on a multiple core CPU in the mid 1980s.
    lol, I ignored that. Just change all the instances of "CPU" in my previous post with "CPU Core", and I think we are ok.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Multithreading
    By Ryan7744 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2014, 04:35 AM
  2. Multithreading question
    By Hikaros in forum Java Theory & Questions
    Replies: 1
    Last Post: November 23rd, 2013, 01:49 AM
  3. Multithreading
    By Miths in forum Java Theory & Questions
    Replies: 10
    Last Post: December 17th, 2012, 02:46 AM
  4. Multithreading Problem
    By Staticity in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 2nd, 2012, 07:39 PM
  5. Multithreading in java
    By skillet in forum Java Theory & Questions
    Replies: 1
    Last Post: March 17th, 2012, 07:32 AM

Tags for this Thread