Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question on threads

 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey..

Thankyou..

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

Thankyou so much..

Regards,
rajani.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic