Results 1 to 11 of 11

Thread: Thread Spawning

  1. #1
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default Thread Spawning

    Hi

    I am writing a simple client/server application. I have a question. Is there any restriction to the number of threads that can be spawned by Java. I mean say for instance x number is allowed and they x number of threads have been created so far by the server. Then will there be any problem in creating another thread.

    Thanx

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    It's OS-dependent. it actually is possible to run out of threads: certain combinations of OS-JVM don't recycle threads properly. I wouldn't worry about it if I were you though, as long as you're using standard OS and standard JVM. we're talking tens of thousands of threads at least, and your program is more likely to run out of memory first.

  3. #3
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default

    Thanx a lot iluxa

    This forum is great. I always get my answer. The reason why I asked this question was that I am implementing a client/server application and for some weird reason there is no socket connection after say 10-14 days. I thought time constrained could be the reason but then that was not right. So then I thought it could be that there is restriction on the number of threads that can be spawned. I am still not sure what could be the reason.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    10-14 days means you're accumulating some stuff somewhere, and you run out in 10-14 days... that's no good. Possibilities:

    - you don't close sockets correctly, and the OS is running out of file handlers
    - you're leaking memory (but then you'd see a OutOfMemory)
    - you're indeed running out of threads, cause OS doesnt' recycle them properly.

    What you could try is a thread pool. A worker thread would look like this:

    Java Code:
    interface Task {
      public void execute();
    }
    class WorkerThread implements Runnable {
      private Task task;
      public void run () {
        while(true) {
          while(task == null) {
            synchronized(this) {
              this.wait();
            }
          }
          task.execute();
          task = null;
        }
      }
      public void setTask(Task task) {
        synchronized(this) {
          this.task = task;
          this.notify();
        }
      }
    }

  5. #5
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default

    Thanx again

    Thats true i dont close socket. Coz i can not close socket coz it runs in an infinite loop. Should I close the socket when the connection is closes which in this case would be in the exception block.

    Thnaks

  6. #6
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    oh absolutely you should!!!

    your system has a total of 65535 sockets, I believe. Once you run out of these, you can't accept any more connections.

    If i were you, I'd close the socket if there hasn't been any activity on it for a while, too (say 30 minutes), depending of course on the logic of your application.

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It sounds like you are definitely running out of file handles. You must close sockets when they are defunct.

  8. #8
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default

    Thanx.

    Every time i have a questions answered on this forum I learn a new thing. So this is what I will do since my application has a continuous client-server interaction I will close the socket when the connection is closed and this can be detected in the catch block in the server code.

    Thanx again

  9. #9
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default

    Hi

    I think the problem is not with the closing the socket. Of course I know sockets have to be closed and I will do that but as of now there is some other problem coz as i was running my applet using netbeans applet viewer the socket connection was established in fact i also printed ut the object inputstream and objectoutstream and both have them were initialized. I think there is some other problem like the object is not returned by the server. Can anyone think out what the problem is????

    thanx

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Well, if you are returning objects from a server, I assume you are opening files to read the contents of the objects? Are you closing all streams related to all files you open, even under error conditioons?

  11. #11
    ersachinjain is offline Member
    Join Date
    Aug 2009
    Posts
    51
    Rep Power
    0

    Default

    Thanx for replying.

    I have modification in my program. Earlier I was not closing the socket and input/output streams. I am closing them now. I will wait for the output and should this wierd error continues I will again post my message. thanx again for replying.


    Thanx to this amazing forum

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  2. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 03:13 AM
  3. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 10:06 PM
  4. Replies: 0
    Last Post: 01-28-2008, 08:02 AM

Posting Permissions

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