Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:00:38+00:00 2026-06-13T18:00:38+00:00

I was thinking about creating a class (like String, StringBuffer etc). This can be

  • 0

I was thinking about creating a class (like String, StringBuffer etc). This can be used in single-threaded as well as in multi-threaded environments. I do not know which kind of environment a developer might be using it. Anticipating the worst-case, I can synchronize.

But,
1. Synchronization takes a performance hit.
2. Without synchronization, it is not thread-safe.

So, I have two options.

  1. Leave the class unsynchronized – but the developer using this class needs to synchronize it whenever appropriate.
  2. Have all synchronized methods – and take a performance hit.

I have seen that many (if not all. for eg, ArrayList over Vector) classes in Java have evolved to take the first approach. What are some of the things I need to consider before deciding on these two options for my class?

Or to put it in a different way, should I use “public synchronized void bar()” over “public void bar()” only when I know for sure that bar can be used in a multi-threaded environment and should not be run at the same time?

EDIT So, clearly I have mis-used the word “utility class” in the heading. Thanks, Jon Skeet, for pointing it out. I have removed the world “utility” from the heading.

To give an example, I was thinking about a class like, say, Counter. Counter is just as an example. There are other ways to implement Counter. But this question is about synchronization. A Counter object keeps track of how many times something has been done. But it can possibly be used in single-threaded or multi-threaded environments. So, how should I handle the problem of synchronization in Counter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:00:39+00:00Added an answer on June 13, 2026 at 6:00 pm

    What I think of as a utility class – typically a grab-bag of vaguely-related public static methods – rarely requires any synchronization. Unless the class maintains some mutable state, you’re usually absolutely fine.

    Of course, if you take parameters which are themselves shared between threads and contain mutable state, you may need synchronization – but that should be for the caller to decide, usually.

    If you mean something else by “utility class” it would be good to know what you mean. If it’s a class with no mutable state (but perhaps immutable state, set at construction) then it’s typically fine to share between threads.

    If it has mutable state but isn’t explicitly about threading, I would typically not put any synchronization within that class, but document that it’s not thread-safe. Typically the callers would need to synchronize multiple operations using multiple objects anyway, so “method at a time” synchronization doesn’t typically help.

    If it’s a class which is all about threading (e.g. something to manage producer/consumer queues) then I would try to make it thread-safe, but document what you mean by that. I’d encourage you not to make the methods themselves synchronize, but instead synchronize on a private final field which is only used for synchronization; that way your class will contain the only code which could possibly synchronize on that object, making it easier to reason about your locking.

    You should almost certainly not make these decisions on the basis of performance. Correctness and easy of is far more important than performance in most cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

was thinking about creating a Single class called 'Request' to handle and clean POST
I'm thinking about creating a mixin form class so that I can add a
I am thinking about creating a new class that does URL shortening that can
I'm thinking about creating some classes along a single-use design pattern, defined by the
I'm thinking about this for a while now, I'm creating a chat application, in
I am thinking about creating my new asp.net mvc web application compatible with native
I was thinking about creating an iPhone app front end for one of our
I am thinking about creating a new repository in SVN for a section of
I've been thinking about creating a forum in PHP so I did a little
I have an ASP.NET project that contains many classes. I am thinking about creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.