• 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

Multithreading: Two errors with education.oracle.com sample exam question

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

Hope everyone is having a great weekend!

I am on the site education.oracle.com going through sample exam questions. It seems I spotted two errors, please clarify if my understanding is correct:



Given:


What is true about possible sets of output from this code?
a) Set 6a 7a 7b 8a and set 7a 7b 8a 8b are both possible.
b) Set 7a 7b 8a 8b and set 6a 7a 6b 7b are both possible. (*)
c) It could be set 7a 7b 8a 8b but set 6a 7a 6b 7b is NOT possible.
d) It could be set 7a 8a 7b 8b but set 6a 6b 7a 7b is NOT possible.

REFERENCE:
JLS 3.0
Option B is correct. Two different Lockdown objects are using the locked() method.




Error number 1

Only one thread can enter locked() at a time because it is synchronized. So how is set 6a 7a 6b 7b possible? I can understand why the other choices are wrong, the jvm is multitasking among three Thread objects, rather than two Thread objects.


Error number 2

When I compiled this code, I got errors due to lines 3, 4 and 6; there was no accessor " . " between the class instance and the start() and getId() method. Below is the corrected code:





 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:Error number 1

Only one thread can enter locked() at a time because it is synchronized. So how is set 6a 7a 6b 7b possible? I can understand why the other choices are wrong, the jvm is multitasking among three Thread objects, rather than two Thread objects.


Ask yourself this - what object is the method synchronized on? Are both threads synchronizing on the same object?

The code definitely needs the additional "."s that you added, so if they were missing from the original that is an error.
 
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

Only one thread can enter locked() at a time because it is synchronized. So how is set 6a 7a 6b 7b possible?


There are two different objects of Lockdown class on which locked locked method would be called. So they won't block each other.

I agree that the code won't compile unless the dots are added...

[Beaten comprehensively by 2 minutes]
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing to watch out for in this sort of question - sometimes they'll make the method static synchronized. In that case it synchronizes on the class, so both threads are locking on the same object, so you get different results. Don't let that catch you out!
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:
There are two different objects of Lockdown class on which locked locked method would be called. So they won't block each other.




AH-HA!

This struck a chord of understanding on my part.

So let me guess, if locked was static, then option b would be incorrect because the method belongs to the class definition, meaning there is one and only "instance" of this method. Is this correct?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:One thing to watch out for in this sort of question - sometimes they'll make the method static synchronized. In that case it synchronizes on the class, so both threads are locking on the same object, so you get different results. Don't let that catch you out!




You beat me by three minutes!
 
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
The quick Brown Matthew jumped over the lazy humans
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so, is 'c' the answer?? think so...
 
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
No C is not the answer, the output "6a 7a 6b 7b" is possible...
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:No C is not the answer, the output "6a 7a 6b 7b" is possible...


Any object that gets in that synchronized method must have to print its id first with 'a' & then with 'b'. so, if the id is 6, 6a must be followed by 6b. only then the lock will be released,right? so the next thread comes and access it - 7a 7b. How come 6a 7a??doesnt that violate the 'synchronized' rules? please enlighten me Ankit.
 
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
Read the first two replies in this thread. The locked method would be invoked on two different objects in the two threads. So both the threads can execute the method at the same time...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic