commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Bourg <ebo...@apache.org>
Subject Re: [lang] new concurrency annotations
Date Wed, 19 Apr 2017 07:17:32 GMT
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: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message