1

The more I learn about multithreading, the more complexities on the way. This is a many times discussed topic but none of the articles provided a clear cut conclusion.String is immutable means that you cannot change the value of a string object but you can make your reference point to a new object. But how does this help in thread safety? Below is test class I created to find an answer for this. The output is inconsistent; at times 'java spring' and other 'java spring spring'. Isn't this happening because the code is not thread safe? This is an expected behavior and I know there are ways to rectify the output. But my issue is how does the immutability of String helps in thread safe execution of code?Please explain.

public class Employee { 
    private String name;
    private String skill;

    public Employee(String name, String skill) {
        this.name = name;
        this.skill = skill;
    }
    public void processEmployee() {

        skill = skill +" spring";
        System.out.println(skill);

    }
    }   
    public class EmployeeRunner {

    public static void main(String[] args) {

        Employee e1=new Employee("Alice", "java");


        Thread t1 = new Thread("t1") {
            public void run() {
                e1.processEmployee();
            }
        };

        Thread t2 = new Thread("t2") {
            public void run() {
                e1.processEmployee();
            }
        };

        t1.start();
        t2.start();

    }

}

Output :

java spring java spring

And sometimes,

java spring java spring spring

Sam
  • 81
  • 1
  • 4
  • 2
    You basically have a potential race condition here, because you don't really know in which order the two threads will hit the `processEmployee()` method. Sometimes, it may happen thread1 followed by thread2, others the two threads could hit at the same time. – Tim Biegeleisen Jul 17 '18 at 01:37
  • 1
    *But how does this help in thread safety?* Being `immutable` is not for this purpose – Scary Wombat Jul 17 '18 at 01:41
  • I am aware of the race condition. But I would like to know on what basis it is said that immutable objects are thread safe because above it is not the case. – Sam Jul 17 '18 at 01:57
  • Thread safety requires that every operation you are performing is thread safe. Having a thread safe object does mean you can't do unsafe operations with it but without thread safety you can even read it safely. – Peter Lawrey Jul 17 '18 at 05:52
  • @JohnKugelman I wish you understood my question before marking it as duplicate within 15 minutes of posting the question because I have spent hours of research before posting it. Immutability and thread safety is a many times discussed topic and I have come across many links including the ones you referenced; but sadly none of them actually discuss the extent to which immutability helps in thread safety. Many places it is simply mentioned that using immutable objects makes your code thread safe.But immutability only eases thread safety and I feel many more discussions need to be done on this. – Sam Jul 17 '18 at 17:12
  • @Sam It would help if you could write up what you've learned from your research and explain why existing questions don't adequately answer your question. I found dozens of duplicates questions covering the same ground. Specifically, how does immutability give thread safety when references to immutable objects can be changed in thread unsafe ways? The two questions I linked do cover that specific aspect. That said, I will grant you that the answers aren't the best. We can reopen your question if you edit it and flesh out the particular points you feel aren't well answered. – John Kugelman Jul 18 '18 at 00:59

0 Answers0