GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ExamLab - thread synchronization with 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 "ExamLab - thread synchronization with "System.out" (3rd test, drag&drop question no. 17)" Watch "ExamLab - thread synchronization with "System.out" (3rd test, drag&drop question no. 17)" New topic
Author

ExamLab - thread synchronization with "System.out" (3rd test, drag&drop question no. 17)

Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi,

resolving the following drag&drop question, I came accross a strange thing, synchronizing code with System.out, instead of MyClass.class or this:


In order to make sure that my assertions concerning two things are correct, here my questions:

1) Is synchronized(System.out) equivalent to synchronized(MyClass.class)? As far, as I know, "System.out” is a static PrintStream object, isn't it?
2) What about the volatile keyword? I suppose it guarantees that both threads are informed when the variable x changes, is this correct?
(I mean communication between both threads, as each thread in Java takes place in a separate memory space)

In any case, I did this exercise wrong only because I've dragged new String("xy") instead of System.out into the synchronization expression ... but I've survived it ;-))


SCJP 6 (88%), SCWCD (89%)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

You can synchronize over any valid Object. While synchronizing you obtain the Object's lock so you can pretty much try obtaining the lock of any Object you so wish.

As for volatile, it is present to ensure that the data is fetched directly from main memory each time.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

1) You can compare synchronized(System.out) and synchronized(MyClass.class). Both will synchronize on an object which has a single copy in a JVM so only one thread can enter such a synchronized block at a time.
2) Volatile keyword is used with variables whose values change frequently. This makes sure that threads don't cache a local copy of the variable so that the thread uses the actual value of the variable at runtime not a cached value...

[Beaten by 2 minutes]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Thank you, guys - your explanations have helped me a lot in order to see the thread issues much clearer!

Besides, ExamLab is a very good testing tool, thank you Deepak:
althoug it's hard to do these mocks (besides, a lot of review work has to be done after each test unit), it's worth ... hopefully, the real exam will be "a piece of cake" compared with ExamLab ;-)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Ulrich Vormbrock wrote:Besides, ExamLab is a very good testing tool, thank you Deepak

I'm guessing Devaka deserves a mention here
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Yes, indeed - I meant:

Besides, ExamLab is a very good testing tool, thank you Devaka!


Sorry for this ;-)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5



Ulrich Vormbrock wrote:Yes, indeed - I meant:

Besides, ExamLab is a very good testing tool, thank you Devaka!


Sorry for this ;-)


I will request Devaka to change his name over the week end
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3112
    
  42



Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Could anyone please explain the role of valatile more clearly !!! Not getting the concept of volatile !!!

Thanks


SCJP 6.0 96%

(Connecting the Dots ....)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

@Sahil, did you try to read a few tutorials on the internet about volatile keyword?? Go through this or this...
 
GeeCON Prague 2014
 
subject: ExamLab - thread synchronization with "System.out" (3rd test, drag&drop question no. 17)