*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question on 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 "A question on threads" Watch "A question on threads" New topic
Author

A question on threads

Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Hi..
I have the following qwestion ...
Can anyone explain me the answer?
___________________________________________________________


What will happen when you attempt to compile and run 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");
}

}

Answer : Compile time error.

Thanks in advance,
Regards,
rajani.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The problem is that you're trying to invoke a constructor that doesn't exist.

Look at the TSamp class. How many constructors do you see? None. That means that the only constructor that class has is the default constructor, which takes no arguments. In your code, you have this line:



This tries to invoke the TSamp constructor that takes a Multi object. No such constructor exists!

If you put this into your TSamp class, it'll work just fine:



That will take the Multi object, which is a Runnable object, and pass it along to constructor in Thread.


SCJP Tipline, etc.
Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Hi Corey..

Thankyou..

Yeah.. there is no constructor
I was a bit confused..

Thankyou so much..

Regards,
rajani.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question on threads