English 中文(简体)
输入Stream(锁定)的书写内容,以备无锁。
原标题:Write contents of an InputStream (blocking) to a non-blocking socket

I m 节目制作简便的Java NIO服务器,头痛很少: 页: 1 InputStreams i need to bens. 我只读了所有文字,这样就产生了一个问题:如果将暂停使用InputStream,all/i>其他联系写作。

我可以使用<条码>InputStream. Available()来检查是否有任何新数据,但如果我已经到达上游结束,那么我似乎知道:mustread()。

这为我创造了一个大的头痛,但我可能认为我是第一个提出这一问题的人。

我提出的唯一选择是:

  • Have a separate thread for each InputStream, however that s just silly since I m using non-blocking I/O in the first place. I could also have a thread pool doing this but then again that limits the amount of simultaneous clients I can pipe the InputStream to.
  • Have a separate thread reading these streams with a timeout (using another thread to interrupt if reading has lasted longer than a certain amount of time), but that ll most certainly choke the data flow should I have many open InputStreams not delivering data.

Of course, if there was a magic InputStream.isEof() or isClosed() then this wouldn t be any problem at all : (

最佳回答

....Have a separate thread for each InputStream, but that s only silly since I m using non-blocking I/O in first place.......

它根本不sil。 首先,你需要检查你是否能够从你的InputStream执行工作中找到一个可选择的通道。 如果你是幸运的,那么你就只能向选择者登记,并且照常办理。 但是,机会是,你的投入机构可能有一个渠道,不是独一无二的渠道,在这种情况下,“为每个投入机构单独铺垫”是显而易见的事情,可能也是正确的事情。

说明SO在上也讨论了类似的问题。 无法从投入流中选取一个可选择的通道。 不幸的是,你 st。

问题回答

I have a single thread performing all writes

你们是否停止考虑这一问题是否属于问题,而不是解决办法的一部分?





相关问题
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 ...