0

As per Java doc, ClosedByInterruptException is thrown when:

  @throws  ClosedByInterruptException
         If another thread interrupts the current thread while the
         transfer is in progress, thereby closing both channels and
         setting the current thread's interrupt status

I want to get some clarification. For example, there is a file which expands occasionally as new data gets added to the end of it. In that instance, does the above line mean that an exception would be thrown if at the time of new data being added, the transferTO method of FileChannel tries to copy the content.

Here, Another thread would be transferTO method of Filechannel Current thread would be that who is trying to write more data to it.

Is that correct?

fscore
  • 2,567
  • 7
  • 40
  • 74

2 Answers2

1

I think that ClosedByInterruptException in the method transferTo() of FileChannel is thrown when other thread calls the interrupt() method on calling thread.

For example:

    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.channels.FileChannel;

    public class Test {

        public static void main(final String[] args) throws Exception {
            final RandomAccessFile fromFile = new RandomAccessFile("d:/pp.dat", "rw");
            final FileChannel fromChannel = fromFile.getChannel();

            final RandomAccessFile toFile = new RandomAccessFile("d:/out.dat", "rw");
            final FileChannel toChannel = toFile.getChannel();

            final long position = 0;
            final long count = fromChannel.size();
            final Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        fromChannel.transferTo(position, count, toChannel);
                    } catch (final IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            final Thread t = new Thread(r);
            t.start();
            t.interrupt();
        }
    }

Relating to the question you make about another thread writing concurrently (if I understand correctly your question), from javadoc:

File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.

The view of a file provided by an instance of this class is guaranteed to be consistent with other views of the same file provided by other instances in the same program. The view provided by an instance of this class may or may not, however, be consistent with the views seen by other concurrently-running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. The exact nature of any such inconsistencies are system-dependent and are therefore unspecified.

rafalopez79
  • 2,046
  • 4
  • 27
  • 23
  • so you are saying that it will work fine for my questions scenario?Another thread would be transferTO method of Filechannel Current thread would be that who is trying to write more data to it. – fscore Nov 25 '14 at 20:07
  • But the exception will obviously occur when you explicitly interuppt it but would it occur with my scenario in the question and if not then what will happen in that case? – fscore Nov 25 '14 at 20:38
  • It won´t be interrupted. One thread will wait for the other, javadoc: Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. – rafalopez79 Nov 25 '14 at 20:45
0

It means the exception is thrown if another thread interrupts this one. It's perfectly clear.

It doesn't say anything about adding new data.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • by another thread interuppting it does it mean the scenario I listed above, one is trying to write and other interuppts by trying to read or thread.interupt – fscore Nov 25 '14 at 20:18
  • It means calling `Thread.interrupt()`. Writing to a file or reading from it doesn't constitute interrupting a thread. Amazing question. – user207421 Nov 25 '14 at 20:38
  • I highly recommend you to only respond to questions when you can answer with no sarcasm. Every person is a beginner at some point so please dont forget that you were there at some point. Thanks for understanding. – fscore Nov 25 '14 at 20:44
  • I highly recommend to you not to make personal remarks here. – user207421 Nov 27 '14 at 21:42