wood burning stoves 2.0*
The moose likes Java in General and the fly likes Multiple static synchronized methods in one class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multiple static synchronized methods in one class" Watch "Multiple static synchronized methods in one class" New topic
Author

Multiple static synchronized methods in one class

Romeo Dickason
Greenhorn

Joined: Jan 31, 2006
Posts: 7
I want to create a utility class that will contain multiple static methods to handle the monotonous task throughout my application.

E.g.:

public static synchronized String checkIfNull(String s) - will return empty "" if null
public static synchronized boolean checkIfError(String s) - will take a String value, return false if null or else parse and return the boolean

etc.

Now because static methods synchronize using the Class object, is the following true?



If a thread calls methodA, and another calls methodB immediately after that, the thread calling methodB will have to wait for the lock on methodA to release as the lock is done at class level?

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Correct.

However, if your class has no data of its own (so no static fields) and also does not rely on any external source (like a database), then synchronization is only slowing down your application. In your example, there is no reason methodA and methodB, or even two parallel calls to methodA, should not be allowed to run together.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Romeo Dickason
Greenhorn

Joined: Jan 31, 2006
Posts: 7
Hi Rob,

Thanks for your reply.

Rob Prime wrote:
However, if your class has no data of its own (so no static fields)

It doesn't

Rob Prime wrote:
and also does not rely on any external source (like a database)

It doesn't

Rob Prime wrote:
then synchronization is only slowing down your application

I agree

Rob Prime wrote:
In your example, there is no reason methodA and methodB, or even two parallel calls to methodA, should not be allowed to run together.

Absolutely!

Let me then explain why I'm considering synchronizing the methods ...

Please have a look and consider the following test and output:



Some output if method is not syncronized: (ouch!)

Entering checkIfNull(): a0.09524904426278391

Entering checkIfNull(): b0.23565355167820223
Returning checkIfNull(): b0.23565355167820223
Returning checkIfNull(): a0.09524904426278391

Entering checkIfNull(): b0.7527650118950606

Entering checkIfNull(): a0.8424380146307968
Returning checkIfNull(): b0.7527650118950606
Returning checkIfNull(): a0.8424380146307968

Entering checkIfNull(): b0.42129968213137825

Entering checkIfNull(): a0.6445911951918275
Returning checkIfNull(): a0.6445911951918275

Entering checkIfNull(): a0.9413244263506504
Returning checkIfNull(): b0.42129968213137825

Some output if method is syncronized: (better!)

Entering checkIfNull(): a0.38640850139032057
Returning checkIfNull(): a0.38640850139032057

Entering checkIfNull(): a0.39237764562271704
Returning checkIfNull(): a0.39237764562271704

Entering checkIfNull(): b0.2756009510370935
Returning checkIfNull(): b0.2756009510370935

Entering checkIfNull(): b0.47526899878499873
Returning checkIfNull(): b0.47526899878499873

Entering checkIfNull(): b0.2636586336838118
Returning checkIfNull(): b0.2636586336838118




Hmmmmmmm ... am I missing something?

Thanks!
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
And what would be the point of such synchronization? If you want to synchronize the threads (eg. to avoid race condition, protect some resources etc.), you should do it externally (ie. in the MyThread class for instance), not in the utility class. Utility class is a kind of a library - you wouldn't want libraries to determine how your application is synchronized, would you? Now, if the only point is to have the log messages be written nicely in order... that's a bit tougher. I'd propose to output the thread ID and/or some other ID for each thread. Some logging frameworks like log4j can do it for you. Then you can brows the logs and trace which call was in which thread. Synchronizing just to get the log entries in order could result in a big performance penalty.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Romeo Dickason wrote:
Rob Prime wrote:
and also does not rely on any external source (like a database)

It doesn't

Yes it does - System.out. That's also an external source, namely the system console. As you need your enter and return messages to be grouped on the system console synchronization all of a sudden makes more sense.
Romeo Dickason
Greenhorn

Joined: Jan 31, 2006
Posts: 7
Thanks Adam and Rob.

Okay, I added the SOP's to test, and maybe jeopardized my own theory if the method should be synchronized or not ...

Basically, I want my methods in the utility class to behave exactly like Math.random().

So it will take a value and maybe do some calc / modification and return a value. No values or resources to consider, or dependencies on external resources.

Is it necessary to synchronize or not?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

No. Two different threads will share nothing at all (well, the CPU maybe ) and have no precedence relation ship (e.g. methodA must always be finished before methodB starts) so you don't need synchronization.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple static synchronized methods in one class
 
Similar Threads
DatabaseManager - design decision
Synchronization on Threads in Java
synchronize question
access modifiers
deciding method from which class to call depending upon parameter passed in