File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multithreading: Two errors with education.oracle.com sample exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Multithreading: Two errors with education.oracle.com sample exam question" Watch "Multithreading: Two errors with education.oracle.com sample exam question" New topic
Author

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

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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:






Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4388
    
    8

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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4388
    
    8

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

Joined: Feb 18, 2010
Posts: 434
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

Joined: Feb 18, 2010
Posts: 434
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

The quick Brown Matthew jumped over the lazy humans
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

so, is 'c' the answer?? think so...


OCPJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

No C is not the answer, the output "6a 7a 6b 7b" is possible...
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multithreading: Two errors with education.oracle.com sample exam question