Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ulrich Vormbrock
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;-))
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Ulrich Vormbrock
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, indeed - I meant:

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


Sorry for this ;-)
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Marshal
Pie
Posts: 4501
268
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone please explain the role of valatile more clearly !!! Not getting the concept of volatile !!!

Thanks
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Sahil, did you try to read a few tutorials on the internet about volatile keyword?? Go through this or this...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic