Win a copy of Head First Android this week in the Android 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Threads Queries

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have following queries about JAVA thread mechanism :-

1) When i say t1.start() in main method then is it sure that now main thread would go back to runnable state ?
I mean is this possible that main thread continues before even executing just one statement
of thread t1 ?

2) When main method calls another threads, is this possible that main thread gets over before
other Threads ?

If this is possible then i have another sub-question

2A)
You must be reading this because you agree that main can gets over before other threads which it started, then...
Suppose we have t1 and t2 as threads in main method. If main gets finsihed
then it implicitly means that its local variables t1 and t2 are vanished.
If t1 and t2 are vanished, then actual Thread Objects and their Runnable Object partners (Job)
forms an "Island of Isolation of Objects" , which are eligible for garbage collection,
This means it may happens that other threads are still active but are eaten by
Java Garbage collector ....???

 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what you are asking would be clearer if you post some code.

Sahil Kapoor wrote:1) When i say t1.run() in main method then is it sure that now main thread would go back to runnable state ?
I mean is this possible that main thread continues before even executing just one statement
of thread t1 ?


I assume this means you have created an instance (t1) of a class that implements Runnable or extends Thread. If you just call t1.run(), you are not running the method in a new thread. You are just executing the method in the current thread. You have to call Thread.start() to call t1.run() in a new thread. And yes, it is possible that your main method will complete before t1.run() is executed.

Sahil Kapoor wrote:2) When main method calls another threads, is this possible that main thread gets over before other Threads?


If you mean is it possible that the main thread runs or completes before the other threads are run then yes, it is possible. And just because a thread terminates does not mean that its referenced objects are garbage collected. An object is never garbage collected until all references to it are gone - even when those references are in different threads.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all i am sorry for typo , it is t1.start() and not t1.run().

t1 is an instance of thread.
Thread t1 = new Thread();

I got your answer to second question but i have further queries about it as follows.

If you mean is it possible that the main thread runs or completes before the other threads are run then yes, it is possible. And just because a thread terminates does not mean that its referenced objects are garbage collected. An object is never garbage collected until all references to it are gone - even when those references are in different threads.



Consider the following code :-




Query :- If in above code main thread gets finished first, then t1 (local variable) defined in main thread would also got vanished. Then reference to Thread object would also get vanished and hence Thread object is abandoned and hence eligible for GC. I am sure it is not like this , and it should not be, but how it is managed ?? What is the actual criteria for GC.

I also want to put it in another way, in the above code with the completion of main method , Dog reference variable d would get vanished and hence the Dog object is eligible for GC, then why not Thread object.



Thanks Tom !!!
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sahil Kapoor wrote:
2A)
You must be reading this because you agree that main can gets over before other threads which it started, then...
Suppose we have t1 and t2 as threads in main method. If main gets finsihed
then it implicitly means that its local variables t1 and t2 are vanished.
If t1 and t2 are vanished, then actual Thread Objects and their Runnable Object partners (Job)
forms an "Island of Isolation of Objects" , which are eligible for garbage collection,
This means it may happens that other threads are still active but are eaten by
Java Garbage collector ....???


Read a definition of 'reacheable object' in the java specification:
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.6.1

A reachable object is any object that can be accessed in any potential continuing computation from any live thread.


Only no reachable objects are eligible for GC.
A thread object is always reachable by a corresponding thread (a thread associated with this object),
so according to the definition the thread object cannot be eligible for GC until its associated thread is alive.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if I were to say that the only objects eligible for GC are those that can't be reached by any live thread?

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ireneusz Kordal wrote:
A thread object is always reachable by a corresponding thread (a thread associated with this object),
so according to the definition the thread object cannot be eligible for GC until its associated thread is alive.



So it means sir, that though t1 reference would vanish, but there is a reference present from another live thread present.
In which variable it is being stored ???
Where it is stored ???

and sir

when we say "thread of execution" then it just means call stack or something like Process Control Block (PCB). or may be (TCB)

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

Sahil Kapoor wrote:

Ireneusz Kordal wrote:
A thread object is always reachable by a corresponding thread (a thread associated with this object),
so according to the definition the thread object cannot be eligible for GC until its associated thread is alive.



So it means sir, that though t1 reference would vanish, but there is a reference present from another live thread present.
In which variable it is being stored ???
Where it is stored ???



Any thread can get access to its thread object via the Thread.currentThread() method. So, if the newly started thread don't have a copy of it, it can get it. And as for where it is stored, that's just an implementation detail. it is just stored somewhere where all active threads objects are stored.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic