This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice 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

Thread has finished but Threads Global Vars are still accesible

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
The problem is that I am using an applets stop method to finish two threads. But when I have stopped the threads and have used the join() method to ensure that they have finished, I can still access the dead threads variables (which I would of thought would have vanished with the thread). This may not seem to be a problem but I am trying to get it so that if the applet start method is called again it will start the same type of thread up again and I think that if these variables are still current that it could cause some quite confusing errors to occur. So if anyone can firstly tell me why these variables are still there and secondly a efficient way of getting rid of a thread so that it's variables go with it.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Thread is an object and as such its members can be accessed even after the thread has finished. For getting rid of a thread it must has been started and finished. As a curiosity threads that were created but not started aren't garbage collected. The problem is that isn't possible to force the garbage collector to run. And anyway, I think your problem are static fields of your own class extending Thread, aren't they?
Are you afraid that they keep their values between start invokations? Try initializing them within the start method.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not exactly. A Thread is not an Object. But typically a Thread has an associated Object. So after the Thread dies, the Object lives on. Kind of like after your body dies, your spirit lives on. But actually your spirit is more like your Thread so its maybe a backwards analogy. anyway.
No the object does not die with the Thread. Further you will have a hard time killing the Object because its tied to the Thread Group which will not die as long as any of its threads live. and if its the default thread group, it will never die.
If you created a Runnable then you probably will not have access to the Runnable object anymore. But if you defined your own extension of Thread, then you will still have access to that objects variables through the ThreadGroup.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by CL Gilbert:
Not exactly. A Thread is not an Object.



Output was true.
Am I missing something here???
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think CL meant to say that a thread is not an Object, but a Thread is. The term "thread" refers to the lightweight process being executed by the JVM to follow a single flow of execution through code; the term "Thread" refers to the Object which is associated with a given execution thread. The Thread object can exist after the execution thread has died.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hehe, you are correct Jim. My capitalization was poor. Wonder how that small thing can play on someone who speaks another language natively?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic