Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By doWhile

Thread: do threads die and go away if there are no references to them?

  1. #1
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default do threads die and go away if there are no references to them?

    If you lose all references to a thread, does it end up getting garbage collected or will it live through to its normal life span?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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.
    DarrylBurke likes this.

  4. #4
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default Re: do threads die and go away if there are no references to them?

    ok thanks, ill check it out

  5. #5
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default 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

  1. About references.
    By fatabass in forum New To Java
    Replies: 8
    Last Post: 03-03-2012, 08:17 AM
  2. Threads per Connection or Threads per Request
    By Felic in forum New To Java
    Replies: 4
    Last Post: 11-22-2011, 09:15 PM
  3. Forward references
    By Norm in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2010, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •