File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads 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 "Threads" Watch "Threads" New topic
Author

Threads

Sharanya Sharma
Ranch Hand

Joined: Jul 26, 2004
Posts: 54
Hi,

Look at the following code

class TSamp extends Thread{
public native String getTime();

}
public class Multi implements Runnable {
boolean bStop;
public static void main(String argv[]){
Multi m = new Multi();
m.go();
}
public void go(){
TSamp ts = new TSamp(this);
ts.start();
bStop=true;

}
public void run(){
if(bStop==true){
return;
}
System.out.println("running");
}

}

The answer to this is given as compile time error during the creating of the new thread in the go method.

As per my understanding a new thread object is created with a runnable as parameter which is quite fine and though the constructor is not defined in this class it would be inherited from the parent Thread class.

Please let me know where I missed.
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
When you say tht the constructor will be inherited from the thread class..its an incorrect statement. Constructors can never be inherited !!

Of course I am assuming you mean that you can invoke thm from the sub-class. Now you are right whn you say tht the Thread constructor is overloaded to take a Runnable object as argument. But whn u pass "this" in the constructor what u are passing is the TSamp object the u just created using new TSamp();

Thread constructors can take a Runnable object but not a Thread object

Atleast as far as I know !!
[ August 10, 2004: Message edited by: Murtuza Akhtari ]

---<br />SCJP 1.4
Sharanya Sharma
Ranch Hand

Joined: Jul 26, 2004
Posts: 54
Hi,

Yae, sorry for that.. I meant that the construtor will invoke the parent class constructor. Or is it that the derived Thread class needs to define its constructors and call the super() constructor from it.

But it is not the Tsamp object that is passed to the constuctor. The this here refers to the Multi object. [go() is invoked on Multi object m]. so we are passing an object of type Multi which implements Runnable as an argument, which is correct.

-Sharanya
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
My BAD !!!

i m awfully sorry ...i didnt see tht the go was called on the Multi object

However the problem is that you are calling the constructor of TSamp(this), passing in an argument of type Multi. but such a constructor is not defined in your TSamp class....the default constructor is a no argument constructor
Solution:

1)Either define a constructor TSamp(Object o) OR
2)Use the Thread constructor as in
Thread b = new Thread(this);

i think this should work
[ August 10, 2004: Message edited by: Murtuza Akhtari ]
Sharanya Sharma
Ranch Hand

Joined: Jul 26, 2004
Posts: 54
Hi,

Yae I too think so.
So does this mean that any class that extends Thread when instantiated to take a runnable interface reference(or reference to a class that implements it) should define its own constructor which will in turn pass that runnable reference to the Thread class(Super class).

-Sharanya
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Thats not necessary....you dont have to define a new constructor unless u want additional features not provided by the Thread(Runnable r) constructor

If u dont want additional features...simply use the constructor from the parent class i.e. Thread

Thread ts = new Thread(Runnable r);
[ August 10, 2004: Message edited by: Murtuza Akhtari ]
Sharanya Sharma
Ranch Hand

Joined: Jul 26, 2004
Posts: 54
Hi Murtuza Akhtari,

Then the code at the very beginning should work fine. But it is not working. Can u please write or make modifications to the above code as u say without adding new constructor.
That would be very helpful

Thx
Sharanya
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Changes are made in bold


[ August 10, 2004: Message edited by: Murtuza Akhtari ]
Sharanya Sharma
Ranch Hand

Joined: Jul 26, 2004
Posts: 54
I wonder how u can refer an oject of Thread to a refernce of Tsamp. Incompatible type.

-Sharanya
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
The crux of the point i made earlier was that "if you do not want additional features"...It was a general statement..not in context to your problem...Since u have a new TSamp class which extends Thread you will want to use the TSamp reference. But it is not necessary...You can make the reference of Thread type too. Just that your TSamp class will not be used to invoke threads...It can be used for other purposes like the native method u have in there. But if u insist on using the TSamp class for invoking threads then yes you will have to have a Constructor in the TSamp class that will take a Runnable object.
Changes are again made in bold



[ August 10, 2004: Message edited by: Murtuza Akhtari ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads