File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324



From Line 6 I understand that two thead instances are being created, one being passed in the construtor argument of another. The thread instance ( as constructor arguments) works on 'r', how about the other thread.
K&B says its an overkill to do something like that, wat exactly does that mean.


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Himalay,

The Thread constructor at hand is Thread(Runnable.) You can pass a Thread object to the constructor because Thread implements Runnable, but the only thing the Thread you are instantiating cares about is the run method. That's why passing a Thread object to the constructor is overkill (as in unnecessary.)


All code in my posts, unless a source is explicitly mentioned, is my own.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324


So by the above statement, shall I interpret that two thread instances are being created and referenced by one variable t1.
One thread "new Thread(new Thread(r))" being utilized and the other thread "new Thread(new Thread(r))" being wasted

Or is it like..the complete statement is useless


Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369



Lost reference is not the only problem that this statement has; API is not used properly here.

As Ruben has already said, the way the API is being used here is incorrect. Unnecessary Thread object is created and passed to the constructor. Moreover, the references to this object is lost.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

Remember Thread class has an instance variable reference look like this:

So if you execute above code:
t1.target=new Thread(r);

and again
new Thread().target=r;

and Thread class has run() method that looks like this:

so t1.run() will call new Thread().run() using polymorphism.
And then new Thread().run() will call "r.run()" using polymorphism,
so we are using an extra new Thread() object unnecessarily that will increase our execution time
as well as take extra space in head to create new Thread() object. Remember here
new Thread() object is being used here, its reference is stored in t1.target so it will
remain there until the thread execution completes.


SCJP 6
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Thank you all, esp Punit..this is what I exactly needed. It took me some time to understand but I feel much better now.
 
Don't get me started about those stupid light bulbs.
 
subject: Threads
 
Similar Threads
thread class interrupted() method
Thread Doubt
Anonymous Issue
Thread Question
Problem in thread