java - When Should I synchronize the methods of my class? -


I was thinking about creating a square (like string, stringbuff, etc.). It can be used in single-threaded as well as in multi-threaded environments. I do not know what kind of environment a developer can use The worst situation I suspect, I can synchronize.

However, 1. Synchronization takes a performance hit 2. Without synchronization, this thread is not secure.

So, I have two options.

  1. Skip to unsynchronize the class - but the developer of this class needs to be synchronized whenever appropriate.
  2. Perform all synced methods - and hit one performance.

    I have seen many people (if not all, for example, array lists on vectors) classes in Java have evolved to take first approach

    or To keep it in any other way, use "public synchronized waste times" ("public" on "public") should be zero times () "Only when I know to ensure That time is used in a multi-threaded environment.

    Edit Therefore, clearly I have used the word "utility class" in the title, thanks, John Skeet, To indicate this, I have removed the world from the title "Utility".

    To illustrate, I was thinking like a class like counter counter is an example to implement counter There are other ways but this question sinks About the encoding. A counter object shows how many times some have been done but it can possibly be used in single-threaded or multi-threaded environments. So, how do I handle synchronization problem in the counter?

    what i think of as a utility class - usually unclear Holding one of the public static methods related to the form - rarely any synchronization is required, until the class retains some instability status, you are completely cured.

    Of course, if you take the parameters that are shared between own threads and are in unstable state, you get synchronization - but this is usually to determine the caller Should be for

    If you want to mean something more by "utility class" then it would be good to know what you say. If this is a class with an uninterrupted state (but perhaps unchanging state is set on construction), then it is usually okay to share between threads.

    If this is a volatile state but not clearly about threading, I usually do not put any synchronization within that class, but the document is that it No thread-safe is usually callers need to synchronize multiple objects using multiple objects, so "method at one time" synchronization usually does not help.

    If this is a class about threading (for example, some / consumer lines to manage the manufacturer), then I try to make it thread-safe, but that document What do you mean by that? I encourage you not to not to create ways to synchronize yourself, rather instead synchronize on a private finals field which only used for synchronization goes; In this way your class will include the only code which may be synchronized to that object, making it easy to think about your locking.

    You definitely do not away

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -