• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on thread

 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers,
below is the code which i ma unable to understand;



please explain the same...
this is not printing anything though its compiling and running fine
 
Faiz A Chachiya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi debasmita,
You have passed the Multi's object to TSamp, but if you see the functionality of run() method, you will come to know that TSamp's run() method is called, which you did not override, So it did not give you any output, Just add a run() method or override the run() method of the Thread class and enter some S.O.P you would be able to see the output.

Faiz.
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi debasmita pattnayak

When you are doing this

TSamp ts = new TSamp(this);
ts.start();


You are invoking the run method of class Thread..
which by its implementation does nothing..

It's because though, you are using constructor of class TStamp
which takes a Runnable object as argument..
But that invokes the Thread() constructer of class Thread..
which is not taking that Runnable object as argument..
and calling it's own implemented run() method
So you are not getting anything on the console..

If you want the run() method of class Multi to be executed..
add this code to your class TStamp

TSamp(Runnable r){
super(r);
}
This way you will be invoking the Thread(Runnable r) constructer of class
Thread.. And hence the run() method of Runnable object will be executed..

Hoping you have got something..

Regards..
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by khushhal yadav:
You are invoking the run method of class Thread.. which by its implementation does nothing..

That is not correct. The run method of the Thread class looks whether there is a Runnable-Object, and if yes the run method of the runnable is called. As result, something like this is possible.
 
Charandeep Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Debasmita Pattnayak

I have modified your code a bit


Actually you have not implemented the run() method in the TSamp class if you don't do then how can you expect it print some output
So I hope this code clears your doubt
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very Sorry Manfred Klug

There are different overloaded constructors of class Thread()..

It's not like always run() method of Runnable object is called..

OK.. Tell me what happens, when we does something like this..

Thread t = new Thread();
t.start();


The native method start() of Thread calls its run() method..
And you get nothing because run() method of class Thread by implementation prints nothing..

Same thing is happening in debasmita pattnayak code..
The actual implementation of

TSamp(Runnable r){
}

is
TSamp(Runnable r){
super();
}


where super();
is the call to [B]Thread() constructor of class Thread..[/B]
which will behave in the same way as i have explained above..
So, you are getting nothing in output..

However in your, you have explained the working of different overloaded constructor of class Thread and i.e. Thread(Runnable r) but it doesn't fit in debasmita pattnayak case..

Hoping, you have got it by now..

Regards..
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by khushhal yadav:
[b]OK.. Tell me what happens, when we does something like this..

Thread t = new Thread();
t.start();

  • A new thread of execution is started
  • The run method of the thread object is called
  • The run method looks whether there is a Runnable object
  • If there is one, the run method of the Runnable is called
  • If not, the run method of the thread object terminates


  • Maybe we are saying the same. But your statement sounds as if Thread.run() does always nothing. And that is not correct.
    [ June 26, 2007: Message edited by: Manfred Klug ]
     
    khushhal yadav
    Ranch Hand
    Posts: 242
    Java Mac Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok..Manfred Klug

    Now if i do the above thing..
    where it will find the run() method..
    unless you provide it a runnable object..right..

    But by having

    Thread t = new Thread();
    t.start();


    we are not provided it with a Runnable object..
    That means run() method of Thread will be executed..
    And run() method of Thread by implementation doesn't print anything..

    And that's what happening in debasmita pattnayak's case..
    This is what i want to convey..

    Hence the above program is printing nothing..

    I am trying to be specific to debasmita pattnayak's problem..
    I am talking in general..

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