This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Synchronization" Watch "Thread Synchronization" New topic
Author

Thread Synchronization

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
Synchronization won work if it is on different objects. Here how it is working? Please explain.....



}
OUTPUT : SCJP-1 SCJP-2 SCJP-3 SCEA-1 SCEA-2 SCEA-3 or SCEA-1 SCEA-2 SCEA-3 SCJP-1 SCJP-2 SCJP-3
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1752
    
    7

If I told you that System.out is a static final reference to a PrintStream instance, and that a PrintStream's print() method uses a synchronized block that synchronizes on its intrinsic lock (using the this reference), could you then explain this behavior?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Just to be more specific than jelle, just look at the synchronization statement

synchronized(s)


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
System.out is a static and if it is locking on this then I think Synchronization works. But there are 2 different anonymous Inner classes. In different classes does the synchronization work???
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Yes you have different anonymous inner classes, but both will share the same instance s i.e. System.out. And since you synchronize on s, so both the run methods in the anonymous inner classes will synchronize on the same object. If I modify your code, then the synchronization will not work



Now both methods will synchronize on different objects...
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Jelle Klap wrote:If I told you that System.out is a static final reference to a PrintStream instance, and that a PrintStream's print() method uses a synchronized block that synchronizes on its intrinsic lock (using the this reference), could you then explain this behavior?

I understand what you are saying, but the statement marked as bold is not relevant for this example, correct?


All code in my posts, unless a source is explicitly mentioned, is my own.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

You are right Ruben. The matter of concern for the question is that the synchronized blocks synchronize on the System.out object. It's not related to the print method...
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1752
    
    7

Ruben Soto wrote:
Jelle Klap wrote:If I told you that System.out is a static final reference to a PrintStream instance, and that a PrintStream's print() method uses a synchronized block that synchronizes on its intrinsic lock (using the this reference), could you then explain this behavior?

I understand what you are saying, but the statement marked as bold is not relevant for this example, correct?


Well, yes and no.
It's important to understand that the output of both run() methods will never intertwine, because both run() methods synchronize on the same lock i.e. System.out's intrinsic lock.
It's also important to understand that, although the print() method also synchronizes on this lock, this is not a dead-lock scenario, because intrinsic locks are reentrant.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Jelle Klap wrote:
Ruben Soto wrote:
Jelle Klap wrote:If I told you that System.out is a static final reference to a PrintStream instance, and that a PrintStream's print() method uses a synchronized block that synchronizes on its intrinsic lock (using the this reference), could you then explain this behavior?

I understand what you are saying, but the statement marked as bold is not relevant for this example, correct?


Well, yes and no.
It's important to understand that the output of both run() methods will never intertwine, because both run() methods synchronize on the same lock i.e. System.out's intrinsic lock.
It's also important to understand that, although the print() method also synchronizes on this lock, this is not a dead-lock scenario, because intrinsic locks are reentrant.

I see what you mean. I wasn't aware of this, but unless you use some specialized API lock related class, aren't locks in Java always reentrant?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Ankit Garg wrote:You are right Ruben. The matter of concern for the question is that the synchronized blocks synchronize on the System.out object. It's not related to the print method...

Thanks Ankit, that's what I meant. But I think Jelle was trying to make a point about locks being reentrant. It's just that it wasn't clear from the wording used.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Thread Synchronization
 
Similar Threads
Threads blocking on static method.... or do they?
Exercise 13.6 in - A Programmer's Guide to Java SCJP Certification - 3rd Ed
Synchronization lock
threads and uncaught exceptions
Runnable to Thread Casting