• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.start() or thread.stop()

 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() ?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic