0

FileChannel channel doesn't has volatile, but boolean closed has volatile. Below is in Java 8 standard library.

public class FileInputStream{
    private FileChannel channel = null;
    private volatile boolean closed = false;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
李泽龙
  • 1
  • 1
  • Is the chanel _reference_ written and read by different threads and all threads always need to see the most recent value? – knittl Dec 18 '20 at 10:37
  • Also, you question is reversed: you state that `channel` has `volatile` (but it does not) and that `closed` is non-volatile (but it is). That's contradictory to the title and the code. – knittl Dec 18 '20 at 10:54
  • @knittl i have fixed the description – 李泽龙 Dec 18 '20 at 11:09
  • @knittl yes, the chanel reference written and read by different threads and all threads always need to see the most recent value.u can see method getChannel and close(). – 李泽龙 Dec 18 '20 at 11:13
  • `getChannel` uses a synchronized block, as [Michael has shown](https://stackoverflow.com/a/65355513/112968). Following the link of the answer explains the effects of volatile and synchronized in the Java Memory Model – knittl Dec 18 '20 at 11:15
  • What is the context of this class? is it in the Java standard library, and is it documented? – scry Dec 18 '20 at 11:43
  • @scry this is in the jdk8 standard library – 李泽龙 Dec 18 '20 at 12:20
  • @knittl I have read "What is the volatile keyword useful for?". I think "FileChannel channel" should be added in this context(jdk8).Is it right? – 李泽龙 Dec 18 '20 at 12:51
  • @李泽龙 See https://stackoverflow.com/a/3519736/112968, especially the part "In practical terms, on current hardware, this typically causes flushing of the CPU caches when a monitor is acquired and writes to main memory when it is released". – knittl Dec 18 '20 at 13:06

1 Answers1

2

The field is only assigned in a synchronized block, so volatile is not required. Visibility is guaranteed by the monitor lock.

public FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
            channel = FileChannelImpl.open(fd, path, true, false, this);
        }
        return channel;
    }
}   

See also What is the volatile keyword useful for?

Michael
  • 41,989
  • 11
  • 82
  • 128
  • public void close() throws IOException { synchronized (closeLock) { if (closed) { return; } closed = true; } – 李泽龙 Dec 18 '20 at 11:01
  • in this method, The field "closed" is only assigned in a synchronized block, but volatile is added – 李泽龙 Dec 18 '20 at 11:04
  • @李泽龙 True but [it was not always that way](https://github.com/openjdk/jdk/blob/5e55e5e2ee8bf0c262a71ea40c3e88ec4effcdad/src/java.base/share/classes/java/io/FileInputStream.java#L325). So maybe it got missed, or authors thought it was somehow "safer" to leave it. In any case, it's irrelevant now because they use AtomicBoolean instead of `close` and `closeLock`. – Michael Dec 18 '20 at 11:31
  • I have read "What is the volatile keyword useful for?". I think "FileChannel channel" should be added in this context.Is it right? – 李泽龙 Dec 18 '20 at 12:49
  • @李泽龙 Nope. I cannot word it any more succinctly than in my answer. All of the desirable traits of volatile are achieved by the use of synchronized. – Michael Dec 18 '20 at 12:54