This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized blocks vs methods.

 
Paul Roubekas
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code synchronized is used in a way I do not understand. What is "synchronized" the Objects "lock1" and "lock2" or the code inside the {}?

[This message has been edited by Cindy Glass (edited September 25, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
The code inside the {} is "synchronized" using the locks on two arbitrary objects "lock1" and "lock2". The objects are not synchronized.
In most cases that you've probably seen the lock being used is implicit -- it is the object's who owns the synchronized method.
------------------
Sun Certified Programmer for the Java� 2 Platform
 
Bill Hause
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code inside the {} is what is synchronized.
No thread can execute the code in the {} unless it has the "one and only" lock on the "mutex" objects "lock1" or "lock2".
lock1 and lock2 simply serve as objects to lock onto. Note they are static so there is only one instance of each across all instances of TestClass.
------------------
 
Paul Roubekas
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right, duh! you can only synch. on one class at a time even if more then one method is synch'ed in the class. So all synch'ed blocks of code must piggieback onto some class.
That clears it up for me, thanks a bunch!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic