Thursday 15 May 2014

java - String thread safety because of immutability? -


Hello was reading about that a string is safe because it is irreversible.

For example, I do:

  string A = "test";   

A thread uses this variable but another thread can change this by using this variable:

  a = a + "something" ;   

So it will change or not?

If it is unstable, then I can get it, that it can be used by one thread at a time. But irrevocable does not guarantee me this?

You are not changing the object indicated by a , but where A :

  string A = "test";   

  a = a + "for a string   

some ";

Here a new string is created as the result of the closing of "test" and "something" , which is "test something " where one digit is a different example.

This is why there is no problem with thread safety, because both sources reference their own one same "test" string objects, but they One of the threads will modify the string to reference the "testomething" object, the other thread will still be referring to the original "test" object

No comments:

Post a Comment