Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

A question on threads

 
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.
 
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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic