synchronizing static methods?

Discussion in 'Java' started by Paul Tomblin, Jul 10, 2003.

  1. Paul Tomblin

    Paul Tomblin Guest

    Ok, dumb question: If I have two static methods in a class, can I make
    them both synchronized to make sure both methods aren't active at the same
    time? Or should I declare a static Object and have synchronized blocks in
    both methods?

    --
    Paul Tomblin <>, not speaking for anybody
    There's going to be no serious problem after this. --Ken Thompson
     
    Paul Tomblin, Jul 10, 2003
    #1
    1. Advertising

  2. Paul Tomblin wrote:
    > Ok, dumb question: If I have two static methods in a class, can I make
    > them both synchronized to make sure both methods aren't active at the same
    > time? Or should I declare a static Object and have synchronized blocks in
    > both methods?


    You can synchronize static methods to prevent more than one thread from
    executing either one or both at a time. This does not, however, have
    any effect on synchronized instance methods. Synchronized static
    methods synchronize on the relevant Class object; synchronized instance
    methods synchronize on the instance. You can also synchronize on a
    static Object if you like (in either static or instance methods); this
    provides means for finer-grained synchronization and / or
    synchronization across instances.


    John Bollinger
     
    John C. Bollinger, Jul 10, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    1
    Views:
    531
    Scott Allen
    Jan 7, 2005
  2. Oliver Wong
    Replies:
    14
    Views:
    1,647
    Chris Uppal
    Jun 13, 2006
  3. Keith Veleba

    Synchronizing methods of a class

    Keith Veleba, Feb 7, 2005, in forum: Python
    Replies:
    3
    Views:
    276
    Keith Veleba
    Feb 9, 2005
  4. sosecret2006
    Replies:
    0
    Views:
    516
    sosecret2006
    Dec 26, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    342
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page