• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

threads

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just came across this in one of the mocks
Que:
You are working on an applet that does animation. There is a single object animC, derived from java.awt.Canvas, which holds all the animation data and a memory image. There is a single Thread, animT, which uses this data to create a new image for the next animation frame and then calls the following method: synchronized void waitForPaint()
{
painted = false; repaint();
while( !painted ){
try{ wait();
}catch(InterruptedException x){}
}
}
The paint method in animC executes the following code after the new animation frame has been shown: synchronized(this){painted = true; notify();} After animT has entered waitForPaint and before the paint method has been executed, which of the following statements is true? [Check all correct answers]
A) Other Threads cannot modify data in the animC object.
B) Other Threads can only modify data in the animC object using synchronized methods.
C) Other Threads can modify data in the animC object using synchronized or unsynchronized methods.
D) If the animT Thread is interrupted, it will exit the waitForPaint method.

Answer

C is correct: Other Threads can modify data in the animC object using synchronized or unsynchronized methods.
Explanation
Answer c is correct. When animT calls wait, the lock on the animC object is released. Answers a and b are incorrect. Other Threads can modify data in animC using any method because animT does not hold a lock on the object while in the wait state. Answer d is incorrect because when a Thread is interrupted, an InterruptedException is thrown. Because this exception is caught inside the while loop, animT will re-enter the wait method.


what I thot was the thread animT wud never lose the lock on animC, since in the code the wait() comes after repaint()(method calling paint()) and so doesnot relinquish control on the object !! also its mentioned that what wil happen after the thread animT enters the synchronized method and before executing the paint() method
Had the call to repaint() been after wait() , then this answer wud have had been fine .
my bet wud be none of these if there was any option

Please help
Thanks and regards
arvind
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arvind,
Im not sure about the answer i will tell u why
but first i'll tell u why itz correct.
the call to repaint will return after calling update and the paint method is not called by the thread executing the repaint method it is called by the AWTthread.And our thread executes the wait() method. I think that is the reason why they gave the answer.
but they cant say before paint() coz paint may be called anytime maybe before our thread.
Discussion on this is welcome
cherry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic