• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Thread question: synchronization on differerent obj

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


You are working on an applet which 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.




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 the wait() method in waitForPaint and before the paint method has been executed, which of the following statements is true?

A. Other Threads can not 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.



I think C and D correct.
Did I take the things correctly?
What do you say?


Source
[ May 01, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

Yes I too think that your answer is correct...


The waitForPaint() method is called by the run of the animT, so it has to be present in the animT class. The method itself is synchornized so its having a lock on the animT thread instance and not on the animC instance.

As there is no lock on animC any method can modify it.

animT can wake up from wait() by Interuppt.

Thanks,
Megha
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Megha,

Sorry but, option :
D. If the animT Thread is interrupted, it will exit the waitForPaint method.

is incorrect!

You missed the boat too with me to find option 'D' as incorrect.
Hey... calling interrupt will throw the InterruptedException.





Result of overlooking the detail!

Thanks for reply,
[ May 01, 2007: Message edited by: Chandra Bhatt ]
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow!!!now I see...these questions really use our set way of seeing questions against us...
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic