• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread has finished but Threads Global Vars are still accesible  RSS feed

 
Tony Combe
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • 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???
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!