• 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

K&B SCJP Chap 9 Q2

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
option F
public void run() { synchronized(System.out) { write(); } }

I don't understand how can you just synchronize on System.out to make the write method always print XY together

public void write() {
System.out.print("X"); //line 1
System.out.print("Y"); //line 2
}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out refers to an object that is an instance of java.io.PrintStream (see the API for class System). You can synchronize your threads on any object you wish, provided all synchronized threads use the same object.
 
Jacky Zhang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
System.out refers to an object that is an instance of java.io.PrintStream (see the API for class System). You can synchronize your threads on any object you wish, provided all synchronized threads use the same object.



But how could synchornize on System.out object make line1 and line2 always run in a row? I could imagine something like System.out.print("Z") from another thread could just sneak in between line1 and line2 because after line1 the lock on System.out will be released as System.out.print("X") finishes, this is all because we synchronize on System.out rather than synchronizing on the class, isn't it?

What am I missing?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jacky Zhang:


But how could synchornize on System.out object make line1 and line2 always run in a row? I could imagine something like System.out.print("Z") from another thread could just sneak in between line1 and line2 because after line1 the lock on System.out will be released as System.out.print("X") finishes, this is all because we synchronize on System.out rather than synchronizing on the class, isn't it?

What am I missing?



hey Jacky Zhang,

Check if its System.out.print() or system.out.println . I hope that is the solution to your problem

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

Originally posted by Jacky Zhang:


But how could synchornize on System.out object make line1 and line2 always run in a row? I could imagine something like System.out.print("Z") from another thread could just sneak in between line1 and line2 because after line1 the lock on System.out will be released as System.out.print("X") finishes, this is all because we synchronize on System.out rather than synchronizing on the class, isn't it?

What am I missing?



If you dig through the Source code, you'll see that the System.out.print ultimately invokes write in the PrintStream class. That code starts out as follows:



So it turns out that every call to System.out.print is synchronized on System.out. That's why no other thread could sneak in there between the two statements.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jacky Zhang:
option F
public void run() { synchronized(System.out) { write(); } }

I don't understand how can you just synchronize on System.out to make the write method always print XY together

public void write() {
System.out.print("X"); //line 1
System.out.print("Y"); //line 2
}




Finally figured it out, context was synchronized in System.out object, since the out object is declared as static, only one instance exists. That is, write() method is under that synchronized context, only when one thread finishes its execution on that context, that's the time other thread can use the System.out object.

Hope this helps.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic