• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Dan's thread question

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

Possible o/p
Prints: X0Y1X2Y3X4Y5. Prints: X0X1X2Y3Y4Y5
From the o/p it is clear that X & Y are taking turn even when getI() method is synchronized.Does Thread.yield() method inside getI() causes to give up lock ,held by thread X?
Thanks
Veena
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Since the yield() method moves the currently running thread to the Ready state I don't think the thread releases the lock. My understanding is that the threads X and Y are taking turns simply because they are two threads running at the same time. Since the getI() method is synchronized after one thread completes it the other thread might get hold of the C object and enter the getI() method. I think the yield() method does not affect the order of Xs and Ys, and I think that any order is possible here.
Thanks,
Alex Stojan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic