• 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

Thread and sleep guarantee

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about this question of Whizlabs exam:



Correct answer: Prints "ABAB" or "BABA"

I know that in practically every situation this will be true. But it isn't guaranted, is it?
It's correct this aswer?

PD: Observe that the synchronized code does nothing because each thread has locks on diferent objects (str is an instace one).
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
joan...
i have a doubt can u please clarify it ..

i ur code..u are creating two threads out of main thread ...
u have a local reference variable ..(even it was an instance variable the output is same )..if there wud have been static then there is only one copy and if u call synchronosied on it ..only one can capture it ..right ..
here is there something like the copy of it being used by each thread ..so even if there a synchronised on it then both threads can execute it concurrently..coz here the output is ABAB or BABA..and not AABB or BBAA..

please anyone clarify ..its
 
Joan Pujol
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are rigth, they execute concurrently

What I'm not sure if you can be sure that it will be ABAB or BABA. Because you can't get any guarantees on sleep, can you?. Then it's possible that while for example A is sleeping B never gets executed, and when A wakes up A gets executed. My question is about this.

I think that in the real world, in practically every implementation of the JVM the result will be ABAB or BABA but, what says the JLS? That is the bible here, isn't it?

Well, today I go for the exam I hope there is no a question like this.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I could be BAAB or ABBA. Or even (nearly impossible) AABB or BBAA.

Sleep may pause the thread for longer time than specified.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic