In a multi-threaded environment shared object can be modified by any of the thread, in some scenarios you may want to ensure that the original object shared among the threads remain unchanged. That can be done by making that object immutable. Since String in Java is immutable by design so it is also thread safe thus a string object can be shared safely among many threads.
Java String immutability and thread safety
One important point to note is that even if String is immutable thus thread safe, reference to the String object is not thread safe.
If String object is passed to a thread and it is modified with in the thread then a new String is created and the reference is changed but the original String remains unchanged. We’ll clear this point with an example.
In the example string object is shared among three threads. While executing these threads, shared string object is modified by appending content to it. When all the threads are finished string object is printed again in the main thread to verify that it remains unchanged.
public class StringThreadSafeExp implements Runnable { private String str; public StringThreadSafeExp(String str){ this.str = str; } @Override public void run() { System.out.println("Executing Thread- " + Thread.currentThread().getName()); // Adding to String str = str + " World"; System.out.println("Modified String " + str); } public static void main(String[] args) { String str = "Hello"; Thread t1 = new Thread(new StringThreadSafeExp(str)); Thread t2 = new Thread(new StringThreadSafeExp(str)); Thread t3 = new Thread(new StringThreadSafeExp(str)); t1.start(); t2.start(); t3.start(); // Wait for all threads to terminate try { t1.join(); t2.join(); t3.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Original String is " + str); } }Output
Executing Thread- Thread-2 Executing Thread- Thread-1 Executing Thread- Thread-0 Modified String Hello World Modified String Hello World Modified String Hello World Original String is Hello
As you can see each of thread, when it modifies the passed String gets reference to a new String object pointing to modified content, leaving the original string unchanged.
Immutability of Java String ensures that String once assigned a value can’t be modified. By using a StringBuffer object which is mutable you can verify what happens when a mutable object is shared among threads and modified.
public class StringThreadSafeExp implements Runnable { private StringBuffer sb; public StringThreadSafeExp(StringBuffer sb){ this.sb = sb; } @Override public void run() { System.out.println("Executing Thread- " + Thread.currentThread().getName()); // Adding to String sb.append(" World"); System.out.println("Modified String " + sb); } public static void main(String[] args) { StringBuffer sb = new StringBuffer("Hello"); Thread t1 = new Thread(new StringThreadSafeExp(sb)); Thread t2 = new Thread(new StringThreadSafeExp(sb)); Thread t3 = new Thread(new StringThreadSafeExp(sb)); t1.start(); t2.start(); t3.start(); // Wait for all threads to terminate try { t1.join(); t2.join(); t3.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Original String is " + sb); } }Output
Executing Thread- Thread-0 Executing Thread- Thread-2 Executing Thread- Thread-1 Modified String Hello World World Modified String Hello World Modified String Hello World World World Original String is Hello World World World
As you can see now the original StringBuffer object itself is modified as it is mutable.
That's all for the topic Is Java String Thread Safe. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Search String in Another String in Java - indexOf, lastIndexOf, contains methods
- Compare Two Strings in Java - equals, compareTo() methods
- Java String matches() Method
- CountDownLatch in Java With Examples
- Java Volatile Keyword With Examples
- Multiple Catch Blocks in Java
- Spring Bean Scopes
- HDFS Replica Placement Policy
No comments:
Post a Comment