Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2008
    Posts
    13
    Rep Power
    0

    Default Difference between StringBuilder & StringBuffer

    Hello Everyone,
    I wanted to know the difference between StringBuilder & StringBuffer classes and thier practical use. So,I wrote 2 code snippets in which I spawn 3 threads simultaneously which make use of StringBuilder & StringBuffer objects. When I run the code, i expect all the 3 threads to run simultaneously in case of StringBuilder & in synchronized manner in case of StringBuffer. But in BOTH the cases, they run in synchronized manner. then what is the use of StringBuffer class?:confused: (In case of String objects, all the 3 threads run simultaneeously). I will share the code snippets for your reference. Please also correct me if I'm wrong in understanding the concept of multi-threading itself. And please, also correct the code.:):)
    Thanks In Advance!

    // StringBuilder...

    public class MultiTread implements Runnable{
    private StringBuilder name;
    public MultiTread(StringBuilder string){
    name=string;
    }

    public void run(){
    for(int i=0; i<=10; i++){
    System.out.println(name.append(i));
    }
    }
    public static void main(String[] args){
    Thread th = new Thread(new MultiTread(new StringBuilder("thread1:")));
    Thread th1 = new Thread(new MultiTread(new StringBuilder("thread2:")));
    Thread th2 = new Thread(new MultiTread(new StringBuilder("thread3:")));

    th.start();
    th1.start();
    th2.start();
    }
    }

    ..................

    //StringBuffer...

    public class MultiTreadBuf implements Runnable{
    private StringBuffer name;
    public MultiTreadBuf(StringBuffer string){
    name=string;
    }

    public void run(){
    for(int i=0; i<=10; i++){
    System.out.println(name.append(i));
    }
    }
    public static void main(String[] args){
    Thread th = new Thread(new MultiTreadBuf(new StringBuffer("thread1:")));
    Thread th1 = new Thread(new MultiTreadBuf(new StringBuffer("thread2:")));
    Thread th2 = new Thread(new MultiTreadBuf(new StringBuffer("thread3:")));

    th.start();
    th1.start();
    th2.start();
    }
    }

    ........

    //String....

    public class MuiltiTreadStr implements Runnable{
    private String name;
    public MuiltiTreadStr(String string){
    name=string;
    }

    public void run(){
    for(int i=0; i<=10; i++){
    System.out.println(name+i);
    }
    }
    public static void main(String[] args){
    System.out.println("main begins...");
    Thread th = new Thread(new MuiltiTreadStr("thread1:"));
    Thread th1 = new Thread(new MuiltiTreadStr("thread2:"));
    Thread th2 = new Thread(new MuiltiTreadStr("thread3:"));
    System.out.println("spawning 3 threads...");
    th.start();
    th1.start();
    th2.start();
    System.out.println("main ends...");
    }
    }

  2. #2
    rpwtdj's Avatar
    rpwtdj is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default

    StringBuilder is compatible with StringBuffer, but with no guarantee of synchronization, and StringBuilder is a little faster than StringBuffer.
    StringBuffer is a thread-safe, mutable sequence of characters.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Same thing discussed in a later thread. :)

    Pooja, since you have two code for two different classes, do a test. JUnit....

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Here is another one,

    StringBuffer and StringBuilder can change their values.

    The only difference between StringBuffer and StringBuilder is that StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.

    Criteria to choose among String, StringBuffer and StringBuilder

    If your text is not going to change use a string Class because a String object is immutable.
    If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
    If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.

    kind regards,
    sukatoa

  5. #5
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default


    Hi,

    Can you give some practical example of this.?



    sanjeev

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    You can have it on the net.... Google is your friend...

    sukatoa.

Similar Threads

  1. StringBuffer situation
    By orchid in forum New To Java
    Replies: 6
    Last Post: 08-12-2008, 01:39 PM
  2. StringBuilder v/s StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 9
    Last Post: 04-11-2008, 09:38 AM
  3. StringBuffer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:33 AM
  4. Difference between ASP and JSP
    By barney in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 07:15 AM
  5. Help with StringBuffer
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 05:50 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
  •