我不确定,但我很确定我在 Oracle Java 实现(1.7.0_67 和 1.8.0_31 我可以确认受影响)中发现了一个错误(或未记录的功能)。
症状
当管道已满时,写入管道的时间可能比管道再次空闲所需的时间长一秒。该问题的一个最小示例如下(我已将此处显示的示例推送到 a repository on GitHub ):
private static void threadA() throws IOException, InterruptedException {
logA("Filling pipe...");
pos.write(new byte[5]);
logA("Pipe full. Writing one more byte...");
pos.write(0);
logA("Done.");
}
private static void threadB() throws IOException, InterruptedException {
logB("Sleeping a bit...");
Thread.sleep(100);
logB("Making space in pipe...");
pis.read();
logB("Done.");
}
pis 和 pos 分别连接到 PipedInputStream 和 PipedOutputStream 实例。 logA 和 logB 是输出线程名称(A 或 B)、以毫秒为单位的时间戳和消息的辅助函数。输出结果如下:
0 A: Filling pipe...
6 B: Sleeping a bit...
7 A: Pipe full. Writing one more byte...
108 B: Making space in pipe...
109 B: Done.
1009 A: Done.
如您所见,B: Done 和 A: Done 之间有 1 秒(1000 毫秒)。这是Oracle Java 1.7.0_67中PipedInputStream的实现造成的,如下:
private void awaitSpace() throws IOException {
while (in == out) {
checkStateForReceive();
/* full: kick any waiting readers */
notifyAll();
try {
wait(1000);
} catch (InterruptedException ex) {
throw new java.io.InterruptedIOException();
}
}
}
wait(1000) 只会被超时(1000 毫秒,如上所示)或调用 notifyAll() 中断,这只会发生在以下情况下:
awaitSpace() 中,在 wait(1000) 之前,正如我们在上面的代码片段中看到的那样receivedLast()中,流关闭时调用(此处不适用)read() 中,但仅当 read() 正在等待空缓冲区填满时 -- 此处也不适用问题
有没有人有足够的 Java 经验来告诉我这是否应该是预期的行为? awaitSpace() 方法被 PipedOutputStream.write(...) 用来等待空闲空间,他们的契约(Contract)简单地声明:
This method blocks until all the bytes are written to the output stream.
虽然严格来说并没有违反,但是1秒的等待时间似乎相当长。如果我要解决这个问题(最小化/缩短等待时间),我建议在每次读取结束时插入一个 notifyAll() 以确保等待的作者得到通知。为了避免同步的额外时间开销,可以使用一个简单的 boolean 标志(并且不会损害线程安全)。
受影响的 Java 版本
到目前为止,我可以在 Java 7 和 Java 8 上验证这一点,准确地说是以下版本:
$ java -version
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
$ java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
最佳答案
这是 Piped*Stream 中众所周知的问题,最终解决方案(针对 JDK-8014239 )是“不会修复”。
JDK-4545831: PipedInputStream performance problems
The class blocks for read when the buffer is empty and blocks for write then the buffer is full. It blocks by calling wait(1000), however a reader will only be woken by a writer who encounters a full buffer (or the wait times out) and a writer will only be woken by a reader who encounters an empty buffer (or the wait times out).
Customer Workaround : Notify()ing the PipedInputStream after every read()/write() would probably solve the problem, but still results in suboptimal performance as many unnecessary notify() calls are being made.
JDK-4404700: PipedInputStream too slow due to polling (alt implementation proposed)
The java.io.PipedInputStream is too slow because it polls to check for new data. Every second it tests if new data is available. When data is available it potentially wastes almost a second. It also has an unsettable small buffer. I propose to consider the following implementation of both PipedInputStream and PipedOutputStream, which is simpler and much faster.
BT2:EVALUATION
We should keep this around as a target of opportunity for merlin and tiger. Due to the age of the classes the submitted code is designed to replace, there may be compatibility issues involved in using it.
JDK-8014239: PipedInputStream not notifying waiting readers on receive
When reading/writing from PipedInputStream/PipedOutputStream pair, read() blocks exactly for one second when new data is written into PipedOutputStream. The reason for this is that PipedInputStream only wakes waiting readers, when during receive() the buffer is filled. The solution is very simple, add a notifyAll() at the end of both receive() methods in PipedInputStream.
It's not obvious how the majority of real-life scenarios would benefit from the proposed change. Per-write notifications may lead to an unnecessary writer stalls. Thus defeating one of the main purposes of a pipe -- time-decoupling readers from writers and buffering. PipedInputStream/PipedWriter API gives us a flexible way to control how often we would like the reader(s) to be notified on new data. Namely, flush(). Calling flush() at the right time we can control latency and throughput.
关于java - 我在 java.io.PipedInputStream 中发现了错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28617175/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa