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?
I think that ClosedByInterruptException in the method transferTo() of FileChannel is thrown when other thread calls the interrupt() method on calling thread.
For example:
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.