• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sharanya Sharma
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changes are made in bold


[ August 10, 2004: Message edited by: Murtuza Akhtari ]
 
Sharanya Sharma
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder how u can refer an oject of Thread to a refernce of Tsamp. Incompatible type.

-Sharanya
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic