Results 1 to 11 of 11
Thread: Thread Spawning
- 05-19-2010, 06:52 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 51
- Rep Power
- 0
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
- 05-20-2010, 01:12 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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.
- 05-20-2010, 05:08 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 51
- Rep Power
- 0
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.
- 05-20-2010, 09:43 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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(); } } }
- 05-20-2010, 09:54 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 51
- Rep Power
- 0
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
- 05-20-2010, 11:47 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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.
- 05-21-2010, 01:01 AM #7
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
It sounds like you are definitely running out of file handles. You must close sockets when they are defunct.
- 05-21-2010, 03:56 PM #8
Member
- Join Date
- Aug 2009
- Posts
- 51
- Rep Power
- 0
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
- 05-24-2010, 05:00 PM #9
Member
- Join Date
- Aug 2009
- Posts
- 51
- Rep Power
- 0
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
- 05-24-2010, 09:15 PM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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?
- 05-24-2010, 09:21 PM #11
Member
- Join Date
- Aug 2009
- Posts
- 51
- Rep Power
- 0
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
Trigger main thread method from secondary thread?
By DigitalMan in forum Threads and SynchronizationReplies: 8Last Post: 01-26-2010, 02:13 AM -
passing a value from parent thread to child thread
By sachinj13 in forum Threads and SynchronizationReplies: 7Last Post: 09-07-2008, 09:06 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks