Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Running a thread's run() method after the thread has completed.

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand the state of a Thread object after the thread has completed. I read in a book that once a thread has completed its run() method, the thread object will still be a valid object on the head and can still be used as an instance object and you can still call methods on it. I decided to try this with the following example:



main() waits for the other 3 threads to complete by using join(). It then call c's run() method but for some reason it is not printing out anything from the run() method. Here is the last few lines of the output after i run the above program.



Even though the thread has completed, i was expecting the value of y to be 400 because of the c.run() call.

Any ideas?

Thanks
 
author
Posts: 23924
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:I am trying to understand the state of a Thread object after the thread has completed. I read in a book that once a thread has completed its run() method, the thread object will still be a valid object on the head and can still be used as an instance object and you can still call methods on it.



Not sure what you mean by this. The JavaDoc states that threads are not restartable -- meaning once a thread has been started, it can't be started again. There is nothing in the JavaDoc that says what happens when you use the thread object in an unspecified way.... which means calling the run() method. All you can do is look at the source code and know what that implementation does, and since, it is not documented, it may be different from JVM to JVM.

However, diving into the source code to look at the implementation....

O. Ziggy wrote:
main() waits for the other 3 threads to complete by using join(). It then call c's run() method but for some reason it is not printing out anything from the run() method.
Even though the thread has completed, i was expecting the value of y to be 400 because of the c.run() call.



On my version of the source code, part of the exiting code will set the target variable to null -- this is the variable that holds the runnable object. So, later when you call the run method, there is no runnable object to call the run() method on, and hence, nothing happens.

Henry

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:On my version of the source code, part of the exiting code will set the target variable to null -- this is the variable that holds the runnable object.


In the Thread class I see

It means that the target instance variable is set to null somewhere. Am I correct in my understanding?
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Even though the thread has completed, i was expecting the value of y to be 400 because of the c.run() call.


When c.run() is called directly, the run method of c i.e, of the run() of the Thread class will be called. Not the run() of DieThread.
And that's the reason you dont see anything printed when c.run() is called and y does not get increased to 400 as you expected. Hope this is clear.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

O. Ziggy wrote:I am trying to understand the state of a Thread object after the thread has completed. I read in a book that once a thread has completed its run() method, the thread object will still be a valid object on the head and can still be used as an instance object and you can still call methods on it.



Not sure what you mean by this.



Sorry i meant to say that it will be a valid object on the heap not a valid object on the head..
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou all..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic