Results 1 to 5 of 5
- 11-24-2012, 06:28 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
-
Re: do threads die and go away if there are no references to them?
An interesting question, I think. I'm sure that Threads just like any object will be marked for GC when they are unreachable by all live threads or static references, but the key question is when will this happen, since it is not something that we can generally control. I believe that calling stop() on a Thread could do this, but doing this is extremely dangerous and could cause unpredictable things to happen to the app which is why the stop() method is deprecated. One way to control this issue is to use a Thread pool so that only a limited number of extra threads are created, and so that inactive Threads can be re-used.
I'd be interested to hear what the pros have to say on this.
- 11-24-2012, 08:30 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: do threads die and go away if there are no references to them?
By creating a Thread, you are inherently creating a reference to the Thread through the ThreadGroup which contains the Thread (typically the ThreadGroup of the current thread). Once the thread terminates, the reference is removed from the ThreadGroup. In other words, a reference is kept to a Thread you start (even if you do not purposely keep a reference), and this reference is kept until the thread terminates. I recommend digging through the source code of the Thread class to verify this for yourself.
- 11-25-2012, 05:11 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
Re: do threads die and go away if there are no references to them?
ok thanks, ill check it out
- 12-05-2012, 10:39 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
Re: do threads die and go away if there are no references to them?
The Thread object won't be collected until you lose reference to it AND the Thread stops running (like doWhile said, while it is active there will be a reference of it elsewhere).
The actual thread (the one the system created) will disappear when run() stops executing.
An example:
Java Code:public static void main(String[] args) throws UnknownHostException, IOException { Thread thread = new Thread() { @Override public void run() { long start = System.nanoTime() + 10_000_000_000L; // Run for 10 seconds then stop while (start > System.nanoTime()) { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Stopping."); } }; thread.start(); while (Thread.activeCount() > 1) { System.out.println("There is another active thread."); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("There is no other active thread."); }Last edited by Potato; 12-05-2012 at 10:48 PM.
Similar Threads
-
About references.
By fatabass in forum New To JavaReplies: 8Last Post: 03-03-2012, 08:17 AM -
Threads per Connection or Threads per Request
By Felic in forum New To JavaReplies: 4Last Post: 11-22-2011, 09:15 PM -
Forward references
By Norm in forum Advanced JavaReplies: 2Last Post: 06-30-2010, 02:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks