Goldman Sachs Interview Question
Java DevelopersCountry: United States
Interview Type: Phone Interview
As we are passing the stringbuilder as a parameter so every time method is called we will get the copy of string builder object...We are not doing anything extra in the method so in method each thread will have a different string builder object.
So we can say the method is threadsafe..Please correct if i m wrong..
Every thread will get its own copy of string builder object and java works as pass by value ...so reference will not get changed....Please correct if i am wrong
public class StringBuilderThreadSafety {
public static void myMethodSafe(StringBuilder sb, String name){
sb = new StringBuilder(sb.toString());
sb.append(" ").append(name);
System.out.println(sb.toString());
}
public static void myMethodUnSafe(StringBuilder sb,String name){
sb.append(" ").append(name);
System.out.println(sb.toString());
}
public static void main(String[] args) {
final StringBuilder sb = new StringBuilder("foo: ");
for (int i = 0; i < 10; i++) {
final int finalI = i;
new Thread(){
public void run(){
myMethodUnSafe(sb, ""+ finalI);
}
}.start();
}
}
}
It all depends what happens inside that method.
It could be either way.
Unsage output :
foo: 1 0
foo: 1 0
foo: 1 0 2
foo: 1 0 2 3
foo: 1 0 2 3 4
foo: 1 0 2 3 4 5
foo: 1 0 2 3 4 5 6
foo: 1 0 2 3 4 5 6 8
foo: 1 0 2 3 4 5 6 8 7
foo: 1 0 2 3 4 5 6 8 7 9
Safe Output:
foo: 0
foo: 6
foo: 4
foo: 5
foo: 3
foo: 2
foo: 1
foo: 7
foo: 8
foo: 9
Since the StringBuilder is mutable object (unlike String) two threads can simultaneously modify it, therefore this method is not thread safe.
You are right . This method is not thread safe, since two thread can pass same object and do the changes which will produce unwanted result.
Immutable objects can have non thread-safe methods (No magics!)
Let's have a look at the following code.
view sourceprint?
01.
public class HelloAppender {
02.
03.
private final String greeting;
04.
05.
public HelloAppender(String name) {
06.
this.greeting = "hello " + name + "!\n";
07.
}
08.
09.
public void appendTo(Appendable app) throws IOException {
10.
app.append(greeting);
11.
}
12.
}
The class HelloAppender is definitely immutable. The method appendTo accepts an Appendable. Since an Appendable has no guarantee to be thread-safe (eg. StringBuilder), appending to this Appendable will cause problems in a multi-thread environment.
source:: java.dzone.com/articles/do-immutability-really-means
it is very well thread safe.provided if this object does not go out of this method.. if it will not go out of this method then it will be local object reference and each thread will have its own copy.. its no more a shared object...
Rectify if I am wrong.
StringBuilder Object reference is passed as an argument. The whole StringBulder instance or Object is not cloned here. Only the value in the reference is "passed by value". For example
StringBuilder sb = new StringBuilder();//--- 1
obj.myMethod(sb);//--- 2
Say in Line 1, sb has a value "zzxccc" or whatever, which points to the memory location of this StringBuilder instance.
Now as line 2 is called, this "zzxccc" is copied and passed by value.
As a consequence, this sb which is local to the method, still refers to the same StringBuffer instance. How can the method be thread safe?
Each thread that executes this code will get it's own parameter list and hence its own references to the passed in StringBuilder. From that perspective, the method call is 100% thread safe.
- graviton6 December 18, 2013now. If 2 (or more) threads call this method passing in the **same** StringBuilder, then that same StringBuilder will be operated on in this method by each executing thread.
Since the StringBuilder is not in itself threadsafe, non-thread safe operations performed by each thread will collide.