Le 19/04/2017 à 00:23, Gary Gregory a écrit :
> I'd like to see a real example before we talk about abstract arguments and
> counter-arguments, which will never end ;-)
Ok, here is a slightly more substantial example. The following class is
immutable but not thread-safe:
public class FileAppender {
private final File file;
public FileAppender(File file) {
this.file = file;
}
public void append(String message) throws IOException {
try (OutputStream out = new FileOutputStream(file)) {
out.write(message.getBytes());
}
}
}
The point is, if the class operates on resources outside the JVM, the
immutability doesn't ensure the thread safety. Immutability is only a
thread safety guarantee for data structures, and not for classes with
operations causing side effects (like writing to files, drawing on the
screen, playing sounds, calling remote services, accessing off-heap
memory, etc).
Emmanuel Bourg
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]