• Post Reply Bookmark Topic Watch Topic
  • New Topic

what's with the ThreadDeath  RSS feed

 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a thread, in which i write stuff into a file.i close the file on run()'s last line and in 3 out of 7 times - if I run the program in debug - I get an exception: ThreadDeath. Why is that, and why does this happen?

here are some bits of code:
 
Mary Wallace
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diane,

Check this web page. That may help you
 
Mary Wallace
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This web page also
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance of ThreadDeath is thrown in the victim thread when the stop method with zero arguments in class Thread is called.
You can check also the API for Thread.stop to get more insights.

./pope
 
Henry Wong
author
Sheriff
Posts: 22865
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really weird... you should not be getting ThreadDeath objects thrown about. It is used by the stop() method to unroll the execution of a thread so that it will exit. This shouldn't happen because the stop() method has be deprecated, and hence, should not be used internally.

Don't have an answer, except we need more details...

Sorry...
Henry
 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
thanks for your replies.

the thing is, I am never calling the stop() method.

all i do is:
so when/why does the thread die?

 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Diana Pop:
Hi
thanks for your replies.

the thing is, I am never calling the stop() method.

all i do is:
so when/why does the thread die?


Threads never die. However, they are taken back to the pool by the JVM, where they are in the luxury of living in the pool...

We may reuse the threads that are taken back to the pool later in the application... Hope this helps...
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ko Ko Naing:


Threads never die. However, they are taken back to the pool by the JVM, where they are in the luxury of living in the pool...

We may reuse the threads that are taken back to the pool later in the application... Hope this helps...




When the run() method of a Thread exits, is'nt it as good as dead?
If we are passing tasks to a 'thread pool', then i understand that one of the free threads is picked from the pool to execute that particular task and that they dont die even after the task is finished and can be reused again.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ko Ko Naing:


Threads never die. However, they are taken back to the pool by the JVM, where they are in the luxury of living in the pool...

We may reuse the threads that are taken back to the pool later in the application... Hope this helps...


This is absolutely wrong - without in the case of ThreadPools usage. Threads are normal objects that are reclaimed by the GC when they are finished.

./pope
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by karthik Guru:

If we are passing tasks to a 'thread pool', then i understand that one of the free threads is picked from the pool to execute that particular task and that they dont die even after the task is finished and can be reused again.


Yep this is correct .

./pope
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Pope:


This is absolutely wrong - without in the case of ThreadPools usage. Threads are normal objects that are reclaimed by the GC when they are finished.

./pope


Sorry... I mean for the case of Thread pool usage... :roll:
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Diana Pop:
Hi
thanks for your replies.

the thing is, I am never calling the stop() method.

all i do is:
so when/why does the thread die?



Two more things Diana:
1. why Thread.sleep() and than myThread.start()? What are your intentions?
2. for non-blocking I/O you can use classes in java.nio package.

./pope
 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

the thing is, in my thread I create the file, containing several records. since I have to create many files, the file-names are composed using System.currentTimeMilis()(HHMMssmmm - hour,minutes,seconds,miliseconds).
Also the records in the file have "item id" generated using System.curentTime...() and they have to be unique in the current file.
Since I cannot have files with the same name, the only solution that came to my mind is to have a small break between starting two threads (=generating two files).

The for(....) that starts the threads is placed in a buttonActionListener.

I found this ThreadDeath error while I was debugging, because I had placed some code after the file.close() statement and it didn't execute. But there was no stack trace, no error listed in my console.
So the application doesn't block or freeze, if I don't use debug, it runs normally and gives me no indication that something might have gone wrong.

Of course, the easy way is to let this statement be the last in the run() method, and call it fixed, but I am hoping I can find a reason for this behavior.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't you do myThread.start(); Thread.sleep() instead of the other way around?

Thread.sleep causes the current thread (your main thread) to sleep. After he sleeps for 100 milis you start another thread. I think you would like to have: start the new thread and make the current one sleep, ain't it?

./pope being confused
 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, you are correct.
although, the filename that needs to be unique is generated in the run(). i need a break between consecutive threads starting, so what is the big difference?
There is a pause - a waiting in the main thread - and then another thread starting. One could argue that if only one file was needed then this delay wouldn't be necessary.

But I am not worried here about performance; from the point of view of the program flow, is there a big difference, can this be the cause of the ThreadDeath?

How about if instead I would have used MyThread.sleep() ? could this lead to generating 2 files with the same name (two threads getting at the same line in the code in the same milisecond)?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

although, the filename that needs to be unique is generated in the run().


oke I see what you mean. You are afraid of the fact that in the same milisecond 2 files can be generated and so the same file name to be used.
However the logic you have described is in code myThread.start(), thread.wait(). (This way the myThread will be able to start immediately ).
Also you can create a method getNextFileName() and pass this parameter to the thread. (getNextFileName() will guarantee that even if two threads request a name in the same milisecond you will be able to provide a unique one).

I am still unable to see why the ThreadDeath occurs.

./pope
 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have discovered the threadDeath

I would like to appologise for not being attentive enough. Indeed there was a call to the stop() for this kind of thread, in a library class used in the project I'm working on, that I hadn't noticed before, in a progress bar listener, that monitored and was able to cancel the current thread.


Thank you all for your helpful messages
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
np... good luck!

./pope
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!