Results 1 to 6 of 6
- 04-16-2008, 07:58 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 13
- Rep Power
- 0
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...");
}
}
- 04-16-2008, 09:06 AM #2
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.
- 04-16-2008, 09:56 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Same thing discussed in a later thread. :)
Pooja, since you have two code for two different classes, do a test. JUnit....
- 04-16-2008, 12:32 PM #4
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
- 04-16-2008, 12:35 PM #5
Hi,
Can you give some practical example of this.?
sanjeev
- 04-16-2008, 12:51 PM #6
Similar Threads
-
StringBuffer situation
By orchid in forum New To JavaReplies: 6Last Post: 08-12-2008, 01:39 PM -
StringBuilder v/s StringBuffer
By Pooja Deshpande in forum New To JavaReplies: 9Last Post: 04-11-2008, 09:38 AM -
StringBuffer
By Java Tip in forum Java TipReplies: 0Last Post: 11-08-2007, 08:33 AM -
Difference between ASP and JSP
By barney in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-07-2007, 07:15 AM -
Help with StringBuffer
By Marcus in forum AWT / SwingReplies: 2Last Post: 07-04-2007, 05:50 AM


LinkBack URL
About LinkBacks

Bookmarks