• 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

Thread constructor

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java 5 API doc it has been mnetioned that:


public Thread(ThreadGroup group,
Runnable target,
String name)
.....
.....
If the target argument is not null, the run method of the target is called when this thread is started. If the target argument is null, this thread's run method is called when this thread is started.



Now, I have modified Question 19 of http://www.danchisholm.net/oct1/topic/section7/threads1.html as follows:



At Line 2 it prints "B" i.e. I do not care about new Thread but I care about only new B()
where as at LINE 4 it prints "A"...but what I thought is it should print "B" because the target is B.

Can somebody explain what is happening here?

Thanks
Murali...
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

you should also have a look at the API documentation for Thread.run().
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody explain line 3 in above code.?
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred

I have seen API for run() method:


public void run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

Subclasses of Thread should override this method.



In Line 3 of above code, thread was constructed using separate Runnable object, so run() method of class B should be called right? Or am I mistaking the wording of the API for run() method?

Murali...
SCJP 5 Aspirant
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Murali Kakarla:
Or am I mistaking the wording of the API for run() method?

You are not mistaking the wording, but you forget that you have overwritten the run method.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please can anyone explain LINE 3

Thread e = new A(new B()); //Line 3
Is it that here casting is done?if yes than please explain now object e will call methods of which class?
in the above given program
Thanks in advance...........
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Klug:
You are not mistaking the wording, but you forget that you have overwritten the run method.



Hi Manfred

Can you please explain which overridden method are you referring to. The run method in the Thread class is


and the private init method in Thread class would simply assign the target as this.target. Now when you would invoke something on this.target like this.target.run(), the run method of the target class should run which is not happening in this case. I would like to know why does the JVM decides to use the this.run() inplace of this.target.run().
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupam Sinha:
the run method of the target class should run which is not happening in this case.

Since in your A class you have overridden the run method. As result, the run method of Thread is never called.
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At Line 1 I have:
Thread d = new Thread(new B()); //Line 1

when I call d.start() it is calling run() method of B because here we are passing B as runnable target. It is not calling run() method of Thread (bold in above code)

At Line 3 I have:
Thread e = new A(new B()); //Line 3

when I call e.start() it is calling run() method of A but here also I have passed B as runnable target. But it is calling run() method of A (bold in above code)

As I stated before, in API also they mentioned that run() method of the runnable target should be mentioned.

This is the confusion here.

Murali...
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Murali Kakarla:
As I stated before, in API also they mentioned that run() method of the runnable target should be mentioned.

And as you have seen, the Thread.run() method calls the run method of the runnable. No call to Thread.run(), no call to Runnable.run().
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Observe following things in the code:

class A extends Thread {
public A(Runnable r) {
super(r);
}

public void run() {
System.out.print("A");
}
}


1. The run() method has been overriden.

2. When you are doing

Thread e = new A(new B()); //Line 3

the e reference is pointing to A's object and calls the run() method in A.
That's why you see output as A.

If you had done
Thread e = new Thread(new B());

then it would have called the original run() and the result would have been as you expected.

Correct me if I am wrong.
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you. I think I understood it now

Murali...
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!! i have understood as in below shown statement

Thread d=new Thread(new B());,this will override the method of Thread class and run() method in class B ie Runnable will be called......

While in the second statement

Thread d=new A(new B);

But here the run method from class A will be called as d is pointing to A's object.

But still have one doubt is it possible if we make A as super class and B extends A than well in the second statement will it override the method in class A and call run method of class B?

please anyone could clear my doubt it will be kinda of you..........

Thanks in advance
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:
Thread d=new Thread(new B());,this will override the method of Thread class

There is no overriding involved here. The Thread.run() method simply calls the run method of B.

But still have one doubt is it possible if we make A as super class and B extends A than well in the second statement will it override the method in class A and call run method of class B?

It's possible, but then your B class extends Thread which may not be desired. There is another possibility to use the run method of B. Simply call the run method of Thread from A's run method.
[ July 14, 2007: Message edited by: Manfred Klug ]
 
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic