aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread" Watch "thread" New topic
Author

thread

b bajwa
Greenhorn

Joined: Oct 26, 2001
Posts: 12
You have two choice:
(1)The thread can execute its own run() method
(2)the thread can execute the run() method of other object
Simon,philip,michel book says that second choice is better
because is the single implementation inheritance rule. If you write a subclass of thread, then it cannot be a subclass of any thing else. Whereas using Runnable, you can subclass whatever other parents class you choose.
Code:
class TestThread3 extends Thread
{
private int i;
public void run()
{
System.out.println("Running"+i);
//yield();
//resume();
System.out.println("Done ");
}
public void xxx()
{
System.out.println("in xxx ");
}

}
class TestThread4 extends TestThread3
{
private int i;
public static void main(String args[])
{
TestThread4 tt=new TestThread4();
System.out.println("value of int"+tt.i);
tt.xxx();
tt.start();
}
}
My doubt is we can make the subclass of TestThread3.
What is the rule means?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
it means that if you subclass direclty the Thread class you cannot subclass any other class. Instead, if you choose to implement the Runnable interface you are free to extend any other class. If you want to implement concurrency in a class but you also have to inherit the behavior of another class then the Runnable interface is the solution. Remember that you cannot extend more than one class in Java, i.e. no multiple inheritance. By extending Thread you loose the possibility to extend another class.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Chris Linde
Greenhorn

Joined: Oct 15, 2001
Posts: 10
From what I have read, one consideration when deciding wether to extend the Thread class or implement Runnable depends on wether you want to be able to pass your object to many different threads over the course of your objects lifetime (in which case it would be a good idea to implement Runnable and pass your object to a different Thread whenever you want). If on the other hand the code in your run() method is tightly coupled to the rest of the code in your object then you may be better off by having your class extend Thread. This is pretty new to me but I believe it is of such importance it should be mentioned.
b bajwa
Greenhorn

Joined: Oct 26, 2001
Posts: 12
Thanks Valentin,Chris, I got what is the meaning of single inheritance.
I have another question regarding simon robert book
"The run() method like any other member method, is allowed to access the private data, and call the private methods, of the class of which it is a member. Putting run()in a subclass of Thread may means that the method cannot get to features it needs"
But i have doubt that these code even can access to private data i of the TestThread3 class. Need Clarification please
Code
class TestThread3 extends Thread
{
private int i;
public void run()
{
System.out.println("Running"+i);

System.out.println("Done ");
}
public void xxx()
{
System.out.println("in xxx ");
}
public static void main(String args[])
{
TestThread3 t3=new TestThread3();
t3.start();
}

}
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
run() is a method like any other and it can access any member of the class it is declared in.
Note that the best way is to run the code and study it !
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited October 31, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread