• 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

Question on Threads (Dan's ideas)

 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, here's a question that I think presents some useful concepts. Dan's approach and techniques involved.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wondering if anybody gave it a try. I thought the concept here is frequently and easily overlooked.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vad
You can always rest assured that there's atleast one person in this world who'll try your examples (after he gets back from work)
I think the correct answers are F, G and I. Somehow, I couldn't find this question in Dan's exam. Could you please give me the Exam & Question no. I want to read his explanation.
Thanks
Harwinder
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Harwinder,
This is not Dan's question strictly speaking. I can give you my most educated "guess" of what's going on here, and it'd be up to you to agree or disagree.
Class NewThread extends Thread and also implements Runnable. It looks like its public void run() is in place. Indeed, it's not needed at all as class Thread already implements Runnable. Class Target also implements Runnable legally and has a legal working run() method. Line Thread nt=new NewThread(new Target(s)); creates an object of type NewThread and polymorphically assigns it to a reference of type Thread. Fine. A new Target object is created and passed as a target Runnable to the NewThread constructor. Now, nt.start() looks like starting a new thread on an anonimous instance of Target. However, in the NewThread class, run() is overridden, so its start() will invoke an empty run() in NewThread instead of run() of Target. The rest is simple: nobody fights for a lock on s object. main() thread enters the synchronized block and hangs there. For good. Nothing is printed. The synchronized block in Target's run is unnecessary. Nobody cares.
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vad. I understand how it works. Just wanted to confirm my answer is correct.
Cheers
Harwinder
 
reply
    Bookmark Topic Watch Topic
  • New Topic