• 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

ExamLab Thread question

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

The code below prints.
call go1
GoIn
call go2
call go3
Go3In

I think it should print
call go1
GoIn
call go2
Go2In (I don't understand why this is not printed. Please help me understand.)
call go3
Go3In

I understand why the code hangs, so no need to explain that part.
Thanks in advance.
Anu

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anu, the reason for Go2In not getting displayed is simple. go1() and go2() are both static synchronized methods. So when go1() is called inside the run method when the first thread is created in main(), then the call tries to join itself so it will wait forever.



Now when the second thread is created in main() method, that time go2() is called in run method.



It will try to get a lock on the class Atr as go2() is a static synchronized method. But the thread created at line 28 holds the lock on Atr class as it called go1() which is also a static synchronized method. This is why the call to go2() will wait for infinite time to get a lock on Atr class...
 
Anu Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it Ankur. Thanks.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anu Bhagat wrote:Got it Ankur. Thanks.



That's my brother's name . My name is Ankit
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Anu Bhagat wrote:Got it Ankur. Thanks.



That's my brother's name . My name is Ankit


Ankur, tell your brother to get off your computer, will you?
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic