English 中文(简体)
我如何使用InterruptibleChannel?
原标题:How do I use InterruptibleChannel?
  • 时间:2012-04-10 11:31:58
  •  标签:
  • java

我试图在安伯实施一条read,使用蓝色通信。 即便在I/O的封锁中,我还是希望打断线的线索,因此,它似乎是一个很好的解决办法:http://docs.oracle.com/javase/1.5.0/docs/api/java/nio/channels/InterruptibleChannel.html”。 我的问题是,该法典应如何看待? 我迄今没有发出任何信息。 这里是:

this.outstream = Channels.newChannel(outstream);
this.outstream.write(message); 

<代码>outstream 是“蓝色体”材料一号材料。

最佳回答

假设你在评论中提到,在一定时间之后将中断频道,你需要处理两个案件,每个案件都有自己的检查例外:

  • Case 1 : Another thread invoked close() on the channel while your thread is blocked on an I/O operation. Your call to write will abort with an AsynchronousCloseException.
  • Case 2 : Another thread called interrupt() on your thread while it s blocking on an I/O operation. Your call to write() will abort with a ClosedByInterruptException.

请更新你的法典,处理这些例外情况:

this.outstream = Channels.newChannel(outstream);
try {
    outstream.write(message);
}
catch(AsynchronousCloseException e) {
    System.out.println("Another thread closed the stream while this one was blocking on I/O!");
}
catch(ClosedByInterruptException e) {
    System.out.println("This thread has been interrupted while blocking on I/O!");
}

Take care to notice that the contract of InterruptibleChannel specifies that these exceptions will only be thrown if the current thread is blocked in an I/O operation. If your thread isn t blocking, then neither of these exceptions will be thrown. In that case, it s likely that if another thread closed the channel you d get a different IOException for trying to write to a closed channel, and if you re not paying attention to the current thread s interrupted status, it will keep writing. However, since behavior for that case is not specified by the interface, it s up to the implementor to decide what to do.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签