*
The moose likes Threads and Synchronization and the fly likes Thread.start() or thread.stop() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread.start() or thread.stop()" Watch "Thread.start() or thread.stop()" New topic
Author

Thread.start() or thread.stop()

sindhu sheela
Greenhorn

Joined: May 20, 2008
Posts: 26
Hai All,

I have come across the below exception.

java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:597)
at com..util.Handler.handleClient(Handler.java:262)
at com..util.Handler.run(Handler.java:164)
at java.lang.Thread.run(Thread.java:619)


In the Handler.java class im creating another thread , and invoking that thread with start() method.
I came across on the internet that,calling run() is a better way ,it might help resolving out of memory error.
Please let me know what might be the other reason (if any) and how can i come across this exception.
real time benifits of using run() over start() method.

Thanks in advance.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

sindhu sheela wrote:I came across on the internet that,calling run() is a better way ,it might help resolving out of memory error.real time benifits of using run() over start() method.


Using run() will not spawn a new thread instead will run in the same thread which used to invoke the run method. I think your topic shoud be changed to thread.run() in place of thread.stop() ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3944
    
  17

Also, the error (OutOfMemoryError) is more about resource management. How many Threads are you creating? How much memory per Thread? It sounds more like you have to find out why you are running out of memory, not find a better way of starting new Threads (though the two may be linked).


Steve
sindhu sheela
Greenhorn

Joined: May 20, 2008
Posts: 26
Thanks for the reply,

Its a tracking application,so when i get the GPRS string i would insert the data through a thread.So the number of threads encounter would be in thousands.I'm using netbeans 6.1 .Thought to look out for the heap dump unfortunately the dump is so huge i can not even have look at it.

Kindly give me some suggestions,that how can i use the threads to work effectively and get out of OutOfMemory exception.


Thanks again .
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3944
    
  17

Working with thousands of Threads is a bad idea. Why do you need thousands of threads? What is the task they are running? More importantly, how long are those tasks supposed to take?

You need to limit the total resource consumption of your application, or provide more resources. One way is to limit the total number of Threads that can be active at one time. You might be able to run a Thread pool of some reasonable size, then when new tasks come in put the task into a queue that the Thread pool executes. If your Thread Pool is busy (too many tasks) you can either reject the request or put it in line to wait for the next available Thread. Look up the ExecutorServices and Java Concurrency tutorial to see how to do this. How useful this approach is depends entirely on what the tasks do and how long they live.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.start() or thread.stop()
 
Similar Threads
Glassfish from myeclipse is throwing exception
Is the following code valid?
start()-Method in Thread class
what is the target of a thread
tomcat startup problem..