• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

threads doubts

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i have some doubts on thread topics... mentioned below ... kindly clear them..


1. in K&B there its wrriten
There�s nothing special about the run()method as far as Java is concerned. Like
main(), it just happens to be the name (and signature) of the method that
the new thread knows to invoke. So if you see code that calls the run()method
on a Runnable (or even on a Thread instance), that�s perfectly legal. But it
doesn�t mean the run()method will run in a separate thread! Calling a run()
method directly just means you�re invoking a method from whatever thread is
currently executing, and the run()method goes onto the current call stack
rather than at the beginning of a new call stack. The following code does not
start a new thread of execution:


is that means that run() can be called as any other method and new stack will only be created by clainng obj.start() method.... ?


===================================

2. in K&B ... on page 505 there written..

When a thread completes its run() method, the thread ceases to be a thread of
execution. The stack for that thread dissolves, and the thread is considered dead. Not
dead and gone, however, just dead. It�s still a Thread object, just not a thread of execution.
So if you�ve got a reference to a Thread instance, then even when that Thread instance
is no longer a thread of execution, you can still call methods on the Thread instance,
just like any other Java object. What you can�t do, though, is call start() again.
Once a thread is dead, it can never be restarted!


got confused......in later there written when thread is dead we can't use it and we can't use its methods but here above written that we still can call method on thread instance ?

remove my confusion ...!!!


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

Originally posted by amit taneja:
is that means that run() can be called as any other method and new stack will only be created by clainng obj.start() method.... ?


That is exactly correct.

Originally posted by amit taneja:
[QB]got confused......in later there written when thread is dead we can't use it and we can't use its methods but here above written that we still can call method on thread instance ?
[QB]


I don't see where they tell us that "when thread is dead we can't use it and we can't use its methods". In fact, some of the methods still work fine after a Thread object is dead, and other methods do nothing or throw exceptions. You can see this phenomenon in action by compiling and running this program:
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its ok and thanx for ur reply

but i m asking that ...first they are saying that if thread becomes dead you can't use it....
in other way they say we can still invoke the methods of object by thread...

i m confused regarding with words or text, seems there is fight of sentencess....

do still remove my confustion...what the text is going to give the message ?
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two concepts involved here: the "Thread object" and the "thread of execution".

The "Thread object" (note the capital T) is simply an instance of the class Thread. It's an object like any other, existing on the heap as soon as you instantiate it, and eventually being garbage collected like any other object. You can call methods on it at any time.

The "thread of execution" (note the lowercase t) is a process handled by the JVM, associated with one Thread object. When the thread of execution finishes, it is considered "dead". It cannot be restarted. However, the Thread object still exists and is still accessible to you if you have a reference to it. Just because the thread of execution ended does not mean you cannot call any methods on the Thread object that you still have. It just means that you cannot start the thread of execution again successfully with the same Thread object.

When Kathy and Bert say that "What you can�t do, though, is call start() again", what I think they mean is that if you call start() again, nothing will happen. No error, no exception thrown, no thread of execution started. Just nothing. If you doubt it, run the program that I posted above. So actually you *can* call start(), and you won't have to handle any error as a result, but there's simply no point in doing do because start() no longer does anything.

Carefully read over the paragraphs you quoted from the book and you'll see that they do not say that you can't call methods on a Thread object after its thread of execution dies. The only method that Kathy and Bert say you can't call is start(). There are other methods you shouldn't call, such as setPriority(int p), which actually does throw an exception. However, most of the methods in the Thread object still work fine after its thread of execution is dead.

Just remember the big difference between "Thread object" and "thread of execution".
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe: When Kathy and Bert say that "What you can�t do, though, is call start() again", what I think they mean is that if you call start() again, nothing will happen. No error, no exception thrown, no thread of execution started. Just nothing. If you doubt it, run the program that I posted above. So actually you *can* call start(), and you won't have to handle any error as a result, but there's simply no point in doing do because start() no longer does anything.

According to the Thread's start() API, it'll throw IllegalThreadStateException if start() is called again.


IllegalThreadStateException - if the thread was already started.



This is a bug in JDK 1.4 and has been fixed in JDK 5.0.

Joyce
[ April 13, 2005: Message edited by: Joyce Lee ]
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joyce. That's good to know. Since the current non-beta exam only covers 1.4, I'm going to assume the exam will entirely avoid this issue of calling start() twice, since we're not expected to know all the bugs in Java.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a ton for reply Joe
...
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic