Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code for isAlive()

 
geetanjali rajput
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why i am using ob1.t.isAlive() ? why not ob1.isAlive().
similarly i am doing for join(). why?
(edited by Cindy to format code - PLEASE indent your own code)
[ April 03, 2002: Message edited by: Cindy Glass ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this doesn't compile, right?
I think you're a little confused about how to create and run a thread. Let me try to re-write this:

Try that.
In order to create a new thread, you need to create an object of type Thread. One of the constructors for a Thread object takes a Runnable object, so you'll pass an instance of the newthread object, which implements runnable.
Then, you can go ahead and start each thread.
Finally, when you want to check to see if a thread is alive, you want to call the isAlive() method on the reference variable you want to check on. The name field has nothing to do with the call.
I hope this helps.
If you have any more questions, please let me know.
Corey
 
geetanjali rajput
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks i appreciate it. i will try this code but please explain the meaning of ob1.t.isAlive() in my code. the code i have written is in a book . i was unable to understand why didn't they used ob1.isAlive().if u think there is nothing wrong in implementing this (ob1.t.isAlive()) stmt. .tell me why.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going to have to post the code from the book your looking at. Like I said in your other post, I'm not sure of the syntax they're using. It almost, but not quite, looks like there is something going on with inner classes here, otherwise there is no need to qualify the method call like you've done in the code.
Post the exact code fom the book and we'll help you out.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I finally understand what is going on. In the original code, you have this:

In this code, the class newthread contains a reference to a Thread object through the variable t. Therefore, when you later decare an object of type newthread and assign a reference to it to ob1, ob1.t is the thread reference inside the newthread object.
There is only one thing missing - the construction of that thread. If you do the following, your program executes and runs.

If you don't first create the Thread object, you'll get a NullPointerException when you try to run it. By adding this line, your code executes fine.
From now on, if you're getting an error, please let us know what the error is. If you would have mentioned a NullPointerException to begin with, I probably could have understood this much faster.
I hope this helps,
Corey
 
geetanjali rajput
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to dave,
this is the exact copy of my code except some s.o.p stmt. in catch block but still if u want the exact code i will write .
to corey,
this code is fine and working. it doesn't show any nullpointerexception.i am confused in, why have the writer used ob1.t.isAlive() why not ob1.isAlive().this example is in the complete reference java2 written by patrick naughton &herbert
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method isAlive is a method of the class Thread. The variable ob1 is of type newthread, which isn't a Thread object, it implements Runnable. Therefore, calling ob1.isAlive() would cause a compiler error because newthread doesn't have an isAlive method defined.
However, the class newthread does contain a Thread object, in the variable t. Therefore, if you access ob1.t, you get a Thread object back. Now that you have a Thread object, you can call isAlive() on it. You could break that single statement into a couple statements, if that would be easier to understand, like this:

When you say that this code is "fine and working," do you mean that it compiles or that it both compiles and runs?
As far as I can tell, it should compile fine, but, if you try to run it, you'll get a NullPointerException. This occurs because the member variable t, in the newthread class isn't initialized explicity, therefore it is initialized implicitly to null. Then, when you make the call to t.start() in the constructor of newthread, you'll get a NullPointerException.
I hope this helps clear things up for you.
Corey
 
geetanjali rajput
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my code is compiling & running. but i really should thank u. thank u very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic