• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Synchronized blocks vs methods.

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic