File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Himalay Majumdar
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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.

 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all, esp Punit..this is what I exactly needed. It took me some time to understand but I feel much better now.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic