Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By allaudin
  • 1 Post By jim829
  • 1 Post By jim829

Thread: StringBuilder Vs StringBuffer

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default StringBuilder Vs StringBuffer

    Can anyone give example how StringBuffer is synchronized .. an example programme that explains this difference . Thanks in advance
    Last edited by allaudin; 07-28-2013 at 05:01 PM.
    Villa likes this.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: StringBuilder Vs StringBuffer

    It's all in the API documentation; what don't you understand from it? I find it a bit strange that you would understand code but no natural language ...

    kind regard,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: StringBuilder Vs StringBuffer

    Try to imagine this. Two separate threads of StringBuilder. The first starts up and begins inserting a huge string of A's. Then the second starts up before the first is finished and inserts a huge string of B's. StringBuider's insert routine is not synchronized, so the A's and B's could be randomly interspersed. If you exchange the StringBuilder with StringBuffer, the synchronization will guarantee the insertion of A's will complete before the insertion of B's.

    Note: I am not talking about inserting a single A or B within a loop multiple times. Once you are out of the insert method, all bets are off. So you need to insert a very large String as an argument. Thus, I did not construct a working example.

    Regards,
    Jim
    Last edited by jim829; 07-29-2013 at 04:09 PM. Reason: YAT
    Villa likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: StringBuilder Vs StringBuffer

    Ok, I was bored so here is how it could happen:

    Java Code:
    public class StringSync {
       public static void main(String[] args) {
          final Buffer buffer = new Buffer();
          final Builder builder = new Builder();
          /**
           * StringBuffer insert emulation
           */
          new Thread(new Runnable() {
             public void run() {
                buffer.insert("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
             }
          }).start();
          new Thread(new Runnable() {
             public void run() {
                buffer.insert("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
             }
          }).start();
          sleep(1000);
          System.out.println();
          
          /**
           * StringBuilder insert emulation
           */
          new Thread(new Runnable() {
             public void run() {
                builder.insert("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
             }
          }).start();
          new Thread(new Runnable() {
             public void run() {
                builder.insert("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
             }
          }).start();
          sleep(1000);
          System.out.println();
       }
       public static void sleep(int mill) {
          try {
             Thread.sleep(mill);
          } catch (InterruptedException ie) {}
       }
    }
      
    
    class Buffer {
       // pretend this is an insert method of StringBuffer
       public synchronized void insert(String str) {
          for (int j = 0; j < str.length(); j++) {
             StringSync.sleep(5);
             System.out.print(str.charAt(j));
          }
       }
    }
    
    class Builder {
       // pretend this is an insert method of StringBuilder
       public void insert(String str) {
          for (int j = 0; j < str.length(); j++) {
             StringSync.sleep(5);
             System.out.print(str.charAt(j));
          }
       }
    }
    Regards,
    Jim
    Last edited by jim829; 07-29-2013 at 05:03 PM. Reason: silly -changed 30 to str.length()
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: StringBuilder Vs StringBuffer

    thanks jim .... I can understand natural language but want to implent it for more clearence josAH

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: StringBuilder Vs StringBuffer

    But do you now understand it, or does Jim's (very nice) example simply demonstrate more clearly to you that the problem exists without you any wiser on how the synchronization actually helps to solve it?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. stringbuffer/stringbuilder help
    By jcarosella10 in forum New To Java
    Replies: 9
    Last Post: 03-09-2012, 04:35 AM
  2. String vs. StringBuilder vs. StringBuffer
    By stchman in forum New To Java
    Replies: 5
    Last Post: 01-12-2012, 08:52 AM
  3. Replies: 3
    Last Post: 03-04-2009, 07:03 AM
  4. Difference between StringBuilder & StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 01:51 PM
  5. StringBuilder v/s StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 9
    Last Post: 04-11-2008, 10:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •