synchronizing static methods?

P

Paul Tomblin

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?
 
J

John C. Bollinger

Paul said:
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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top