0

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.

// 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...");
}
} 
erickson
  • 265,237
  • 58
  • 395
  • 493
Psl
  • 3,830
  • 16
  • 46
  • 84
  • Google, first result is: http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-java – Enigma Oct 09 '13 at 06:25

3 Answers3

8

You are using diferents instances of StringBuffers and StringBuilders per thread. To see the synchronization, you must use same instance of object in all threads ;-)

angel_navarro
  • 1,757
  • 10
  • 11
  • In fact, the only threading behavior being tested by OP's code is `System.out.println()` - everything else is running in its own thread. – dimo414 Oct 09 '13 at 13:17
2

A description of expected versus actual behavior would be helpful, but I'm guessing that you saw sequential output from each thread, but expected to see output interleaved from different threads.

Thread scheduling depends on the system. There is no guarantee that threads will be scheduled fairly (with threads of equal priority getting "time slices," for example), or that threads will run concurrently, even on a multiprocessor system.

Also, remember that System.out.println() is synchronized, and contention for intrinsic locks isn't guaranteed to be resolved fairly or immediately. So it's possible that one thread repeatedly acquires the lock on System.out, and other threads don't get a chance to print until that thread is finished. You can get "fair" locking (with a possible performance penalty) using a java.util.concurrent.ReentrantLock.

Another possibility is that your system is so fast that one thread finishes before the next thread even tries to start running. More iterations would help detect that case.

erickson
  • 265,237
  • 58
  • 395
  • 493
0

You should have used same instance of StringBuilder/StingBuffer for all thread. so that you can see synchronization.

Anuj Aneja
  • 1,323
  • 11
  • 13