• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple static synchronized methods in one class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Romeo Dickason
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic