1

In this tutorial it is given that StringBuffer is synchronized and StringBuilder is not

  1. Can a class be synchonized ?

  2. I know that synchronized methods are locked for particular thread and non synchronized are not.But how does a performance of the programe is increased with non-syncronized method?

  3. Code from here

     public class Main {
        public static void main(String[] args) {
            int N = 77777777;
            long t;
    
        {
            StringBuffer sb = new StringBuffer();
            t = System.currentTimeMillis();
            for (int i = N; i --> 0 ;) {
                sb.append("");
            }
            System.out.println(System.currentTimeMillis() - t);
        }
    
        {
            StringBuilder sb = new StringBuilder();
            t = System.currentTimeMillis();
            for (int i = N; i --> 0 ;) {
                sb.append("");
            }
            System.out.println(System.currentTimeMillis() - t);
       }
      }
    }
    

    gives the numbers of 2241 ms for StringBuffer vs 753 ms for StringBuilder.

and accoding to this the extracode is the synchronised keyword in StringBuffer.Does this synchronised keyword cost the extra 1488ms and if so then should we always prefer StringBuilder instead of StringBuffer?

Thanks in advance.

Community
  • 1
  • 1
deogratias
  • 472
  • 2
  • 8
  • 23
  • @Pablo its not a duplicate,i know the difference between StringBuffer and StringBuilder ,i asked about performance issue in both rather than difference. – deogratias Jul 18 '14 at 09:37
  • The difference is precisely the performance. StringBuffer is slower because of the synchronization logic, even if you are not using that feature. Answers in that question address that – Pablo Lozano Jul 18 '14 at 09:43

3 Answers3

1

No, a class cannot be synchronized. It should say that StringBuffer is thread-safe instead.

Synchronizing involves extra steps, so there's obviously a tiny performance hit. However, that's not where you want to start optimizing. Also, uncontended synchronizing is supposed to be fast, so using a StringBuffer instead of StringBuilder in most cases wouldn't make any difference.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • "tiny" is in the eye of the beholder. – Solomon Slow Jul 18 '14 at 19:40
  • @jameslarge well, often the less experienced people hear "synchronization is expensive" and they think that it's the first place they need to start optimizing. Never mind that the database doesn't have any indexes, let's improve performance by changing all the `StringBuffers` into `StringBuilders`, whee! – Kayaman Jul 18 '14 at 19:47
0

Can a class be synchonized ?

Yes, you can synchronize on the class Object (Assuming you meant the class Object). When you synchronize on the static fields or static members of a class, then the Class Object itself will be synchronized. So, no two threads can access the class level fields / methods at the same time.

I know that synchronized methods are locked for particular thread and non synchronized are not.But how does a performance of the programe is increased with non-syncronized method?

Synchronization comes at the expense of locking and releasing the Object's monitor, it also prevents code restructuring by JVM (done for better performance).

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0
  1. I'm not sure what you mean by "Can a class be synchronized?" but in general classes can be designed to be thread safe. This usually has some performance penalty involved. Using the synchronized keyword is the most basic way of achieving thread safety.
  2. The exact performance penalty will depend on lots of things. One of the answers to the question you linked to gives a benchmark of StringBuffer versus StringBuilder.
brain
  • 5,496
  • 1
  • 26
  • 29