+7  A: 

Thread safety is about protecting shared data and immutable objects are protected as they are read only. Well apart from when you create them but creating a object is thread safe.

It's worth saying that designing a large application that ONLY uses immutable objects to achieve thread safety would be difficult.

It's a complicated subject and I would recommend you reading Java Concurrency in Practice which is a very good place to start.

willcodejavaforfood
Not difficult per se, but a solid understanding of FP is necessary and using a functional language makes it much less painful.
delnan
Actually, it is possible to create an object in a way that isn't thread safe. Starting a thread from the constructor is a good way to break thread safety, for example, if the new thread has a reference to the object that you haven't finished constructing.
Bill Michell
@Bill Michell - Yes, and this is something that is covered by Java Concurrency in Practice. You have to be aware of how to use this in the consturctor.
willcodejavaforfood
Immutable objects won't save you from incorrect behavior -- just _some_ (not all) of the difficult to diagnose problems that can happen with threads.
Justin
A: 

Ok ok, it is true, but i still insist that in Java it is so complex that you can treat it as "no".

Immutable just mean that if you want to modify you must create new instance. All operations on thread global variables are not thread safe.

Let me explain. Java is imperative language so i would really like to see how you create application that consists of only immutable types. What about counters for example? How to you plan to increment something immutable?

So you can't make Java app with only immutable types, you will stop at some time and use some mutable. And here you are not thread safe.

It is possible to live with only immutable types if you are using functional language, like Haskell, but Java is not that functional.

Andrey
Immutable objects are thread-safe so a program only using immutable objects to share state would be thread safe
willcodejavaforfood
@willcodejavaforfood can you proove that? Objects are immutable, but variables are always mutable
Andrey
Concurrency in practice: Brian Goetz - "Immutable objects are always threadsafe."
Jaydee
Depends on whether "final" is implied by "immutable." It may be, but if you have non-final immutable objects, you're quite right.
BlairHippo
@Jaydee question was not Are immutable objects threadsafe, but "Java program would be thread safe" see the difference? and my answer is no, because you can't use only immutable objects in Java.
Andrey
You can write a program in java that doesn't mutate any values. But it isn't easy. You can't use loops, for example - you have to use recursion. But if all you want is to be thread safe, your constraints aren't quite that bad.
Bill Michell
@BlairHippo yes, this is what i meant. Then i explained why you can't live with only immutable objects and non-final fields.
Andrey
@Bill Michell you can write simplest apps that way. But you can't serious apps that way. and how threads will interoperate then if everything is immutable?
Andrey
Concurrency in practice: Brian Goetz - "An object is immutable if: 1. its state cannot bemodified after construction. 2. All its field are final; and 3. It is properly constructed (the "this" reference does not escape during construction).
Jaydee
@Andrey: You're mistaking "can" for "should." CAN you make a Java program that uses nothing but final immutable variables? Sure. Can you make a USEFUL program that way? ... I'm not gonna say "no," but it'll take a much smarter code monkey than me, that's for sure.
BlairHippo
@Andrey - 1) variables that are NOT object or class attributes are only visible to the current thread. They are by definition thread-safe ... whether mutable or not. 2) Variables are not types.
Stephen C
@Andrey: The question was "Is it true that if I only use immutable data type, my Java program would be thread safe?" note the "only".
Jaydee
@Andrey: Have a look at purely functional programming languages. They get shit gone (and often in very clever ways) without mutating any variables, let alone values. And they use parallelism (including, but not limited to threads).
delnan
@BlairHippo @Jaydee i am not that good at java threading model (i am a .net developer) but i have a feeling that you will have problems using only immutable stuff there. just try to write simple app that count primes in multiple threads. i doubt that that will be easy and even possible. then try with mutable types.
Andrey
@delnan well, if i refer to Haskell means that i *took a look* at pure FP languages. and I say that Java is not that functional (as Haskell)
Andrey
@Stephen C well, theoretically you can. but it might be not reasonable. i played with immutable types in C# (note that C# version >= 3.0 is much more functional than java) and it was very ugly.
Andrey
@Andrey: I write multithreaded Java most days, and you are quite correct, using only immutable types would seriously cramp my programming style, but that wasn't the question. I recommend the use of local variables and synchronize in practical java programming.
Jaydee
@Andrey: Then you should know that it's perfectly possible to get along without mutability. In every language. Just like OOP is possible (but a pain) in C, it's possible (but a pain) in e.g. Java.
delnan
@Andrey: I think we're on the same page. I believe the short answer to the question is "Technically 'yes,' but good luck coming up with a Java program that isn't a toy under that restriction."
BlairHippo
@Jaydee if you are good at Java threads could you explain how threads can communicate to each other in strict immutable fashion? i really can't get it.
Andrey
@Andrey: threads can not communicate using immutable objects... immutable objects don't change state so there is no information to communicate.
Jaydee
@Jaydee so can you write Java app that will count primes up to specific number? And then show it as single number?
Andrey
+2  A: 

It is true. The problem is that it's a pretty serious limitation to place on your application to only use immutable data types. You can't have any persistent objects with state which exist across threads.

I don't understand why you'd want to do it, but that doesn't make it any less true.

Details and example: http://www.javapractices.com/topic/TopicAction.do?Id=29

Erick Robertson
+1  A: 

If every single variable is immutable (never changed once assigned) you would indeed have a trivially thread-safe program.

Functional programming environments takes advantage of this.

However, it is pretty difficult to do pure functional programming in a language not designed for it from the ground up.

A trivial example of something you can't do in a pure functional program is use a loop, as you can't increment a counter. You have to use recursive functions instead to achieve the same effect.

If you are just straying into the world of thread safety and concurrency, I'd heartily recommend the book Java Concurrency in Practice, by Goetz. It is written for Java, but actually the issues it talks about are relevant in other languages too, even if the solutions to those issues may be different.

Bill Michell
As the question is asked, you could increment a loop variable which existed only within the local function. This would be completely thread-safe as the variable would only exist on the stack for that thread.
Erick Robertson
Actually, the question asks about a program that only uses immutable data types. There *are* ways to write thread safe java programs with mutable variables, and JCIP talks about most of them. But I believe I did answer the question asked.
Bill Michell
A: 

Immutability allows for safety against certain things that can go wrong with multi-threaded cases. Specifically, it means that the properties of an object visible to one thread cannot be changed by another thread while that first thread is using it (since nothing can change it, then clearly another thread can't).

Of course, this only works as far as that object goes. If a mutable reference to the object is also shared, then some cases of cross-thread bugs can happen by something putting a new object there (but not all, since it may not matter if a thread works on an object that has already been replaced, but then again that may be crucial).

In all, immutability should be considered one of the ways that you can ensure thread-safety, but neither the sole way nor necessarily sufficient in itself.

Jon Hanna
A: 

Although immutable objects are a help with thread safety, you may find "local variables" and "synchronize" more practical for real world progamming.

Jaydee