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 5 of 5

Thread: Threads question

  1. #1
    Junior Member Randor's Avatar
    Join Date
    Nov 2012
    Location
    Olean, NY
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Threads question

    Hello all,

    I am writing a program that will be multi-user and have implimented sockets, one of the things I will have the program do is scan the Arraylist of sockets and check to make sure they are still connected, and if not then remove them from the program.

    My question is, should I do this in a seperate thread or put it loop into the main game while true loop?

    I ask this because I already use 2 seperate threads (the main flow loop and a socket connection loop) and I don't yet know if if there is such a thing as too many seperate threads running at the same time?

    My assumption is that the threads are less strain on the program than a linear writing style but I was hoping for some clarification..

    Thank you for any advise

    Randor
    When I die, I want it to be peacful and in my sleep, like my grandfather. Not flailing and screaming like the people in his car!!


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

    Default Re: Threads question

    There can be such as thing as too many threads, but this is typically if one spawns dozens or hundreds for tasks that should only need a few. If you feel (or have evidence) that the work will bog down any other important process in the same thread, then create a new thread to do the work in parallel. In other words, yes create a new thread.

    I think to best address your concern is to do some trial and error in a controlled environment. Create a small program which performs some intense operations and play with the number of threads it uses - watch the processors through your OS and see how the number of threads affects the strain on your OS.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Threads question

    To add to copeg's excellent advice, one way to control the number of threads is to use one of the various shades of thread pool, and you might consider using this for the threads that the server uses to communicate with its many sockets.

  4. #4
    Junior Member Randor's Avatar
    Join Date
    Nov 2012
    Location
    Olean, NY
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Threads question

    Thank you very much...
    When I die, I want it to be peacful and in my sleep, like my grandfather. Not flailing and screaming like the people in his car!!

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Threads question

    If you are memory constrained watch out on creating too many threads. As suggested use a thread pool if needed - also check the thread stack size.
    Need Confluence & JIRA hosting? Query Foundry | Need Shared Hosting & Virtual Servers? Cloud Shards
    Ultra fast and reliable USA VPS!

Similar Threads

  1. Use of threads in an animation
    By 2by4 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 15th, 2011, 09:47 AM
  2. Need explonation of this question about threads!
    By lulzimfazlija in forum Threads
    Replies: 6
    Last Post: April 21st, 2011, 10:15 AM
  3. threads
    By crazed8s in forum Threads
    Replies: 2
    Last Post: December 14th, 2010, 05:33 AM
  4. Threads and JDBC
    By shorawitz in forum Threads
    Replies: 1
    Last Post: November 11th, 2010, 09:24 PM
  5. Servlet Threads Question
    By doNotPost in forum Threads
    Replies: 1
    Last Post: October 21st, 2010, 09:19 AM