简单的异步I / O:许多线程,一个文件(Simple asynchronous I/O: many threads, one file)
我有一个科学的应用程序,我通常与
xargs
并行运行,但这个方案会导致重复的JVM启动成本,并忽略缓存的文件I / O和JIT编译器。 我已经调整了代码来使用线程池,但是我一直在保存我的输出。程序(即新程序的一个线程)读取两个文件,执行一些处理,然后将结果打印到标准输出。 目前,我已经通过让每个线程将其结果字符串添加到
BlockingQueue
来处理输出。 另一个线程从队列中取出并写入文件,只要布尔标志为真即可。 然后我awaitTermination
并将标志设置为false,触发文件关闭并退出程序。我的解决方案似乎有点杂乱; 什么是最简单和最好的方式来实现这一目标? 我应该如何将来自多个线程的主要结果数据写入单个文件?
例如,如果答案是广泛适用的方法,则答案不一定是Java特定的。
更新
我正在使用“STOP”作为毒丸。
while (true) { String line = queue.take(); if (line.equals("STOP")) { break; } else { output.write(line); } } output.close();
我手动启动队列消耗线程,然后将作业添加到线程池,等待作业完成,最后中毒队列并加入使用者线程。
I have a scientific application which I usually run in parallel with
xargs
, but this scheme incurs repeated JVM start costs and neglects cached file I/O and the JIT compiler. I've already adapted the code to use a thread pool, but I'm stuck on how to save my output.The program (i.e. one thread of the new program) reads two files, does some processing and then prints the result to standard output. Currently, I've dealt with output by having each thread add its result string to a
BlockingQueue
. Another thread takes from the queue and writes to a file, as long as a Boolean flag is true. Then IawaitTermination
and set the flag to false, triggering the file to close and the program to exit.My solution seems a little kludgey; what is the simplest and best way to accomplish this? How should I write primary result data from many threads to a single file?
The answer doesn't need to be Java-specific if it is, for example, a broadly applicable method.
Update
I'm using "STOP" as the poison pill.
while (true) { String line = queue.take(); if (line.equals("STOP")) { break; } else { output.write(line); } } output.close();
I manually start the queue-consuming thread, then add the jobs to the thread pool, wait for the jobs to finish and finally poison the queue and join the consumer thread.
原文:https://stackoverflow.com/questions/6637357
最满意答案
好吧,测试平台确实存在问题。 我和同学一起在实验室里练习了。 谢谢你的帮助,无论如何,非常感谢。
ps我所做的只是在最底部的测试平台中改变了一些时序值,当负载和换档位发生时,我让它工作。
Well it was indeed something in the testbench that was giving issues. I worked it out in the lab with fellow classmates. Thank You for your help anyways it is much appreciated.
p.s. All I did was changed some timing values in the testbench at the very bottom to when the load and shift bit would happen and I got it to work.
相关问答
更多-
此问题的解决方案是使用半加法器和完整加法器代替上面显示的加法器。 它使用了以下逻辑: 完全使用: 64 AND门; 8个半加法器; 48个全加器。 The solution for this problem was use half adders and full adders in the place of the adder shown above in the question. It was used the logic below: Totally were used: 64 AND gates; ...
-
您在问题中暗示的地方与并发分配或顺序语句无关。 它与if和case之间的区别更多。 在我们开始之前,让我们了解一些等价物。 并发条件赋值: Y <= A when ASel = '1' else B when BSel = '1' else C ; 与具有以下代码的进程完全等效: process(A, ASel, B, BSel, C) begin if ASel = '1' then Y <= A ; elsif BSel = '1' then Y <= B ; el ...
-
这在VHDL的上下文中似乎是一个有效的问题。 电源使用由两个因素组成,静态和动态。 前者与您的设计使用的资源区域有关,后者与您设计中每秒信号的转换次数有关。 这种动态功率是由许多因素造成的,例如晶体管中的线路损耗,以及设计中的导线将具有电容元件的事实。 在您建议的极低速度下,静态功耗将占主导地位,因此采取的方法可能是减小设计的尺寸。 例如,有许多已发布的位串行乘法器设计。 使用加法器,根据操作数的宽度/数量,您可以专注于使用压缩tress作为预处理步骤。 这些当然只是建议,很多将取决于您的目标硬件。 It ...
-
这是让您开始完成作业的内容。 它接受月份的二进制值1-12,如果是闰年,则输出该月份的天数。 这是在没有时钟(组合/异步逻辑)的情况下完成的。 我认为您可以采用这一点并确定使用顺序语句根据您的分配要求创建替代实现的最佳方法。 library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity Days_In_Month is port ( I_MONTH : in unsigned(3 ...
-
您的问题有两种可能的解释: 情况#1:您的设计一次只使用两个可能的组件中的一个。 决定使用哪个组件是在编译时完成的,即它是在代码中编写的,在合成电路后不可能改变。 情况#2:您的设计同时使用这两个组件,并使用信号动态选择其中一个可能的输出。 每种情况都有不同的解决方案。 情况#1的解决方案:在您的实体中使用generic ,并在架构体中使用if-generate语句。 这是一个例子: entity component_selection_at_compile_time is generic ...
-
什么是用于VHDL的标签?(What is labels used for in VHDL?)[2022-01-24]
标签用于识别。 例如IEEE1076-2008说 7.3.1总则 配置规范将绑定信息与代表给定组件声明实例的组件标签相关联。 考虑下一段代码: entity e is end entity; architecture a of e is begin process is begin wait; end process; foo: process is begin wait; end process; end architecture; 在模拟中(使用modelsim),这将显示为 即标签f ... -
你的代码有一些问题: next_state和state不属于敏感列表 写入CLK'event and CLK = '1'应由rising_edge(CLK) state <= state; 没有效果,并导致像ISE这样的工具误读该模式。 去掉它。 在操作员周围放置空间不会造成伤害,但会提高可读性。 你为什么期望a-b的结果位30到61而不是0到31? state和next_state不代表状态机的状态。 这只是一个注册。 改进的代码: architecture Behavioral of Multiplie ...
-
调试VHDL:如何?(Debugging VHDL: How to?)[2023-05-29]
正如其他文章指出的那样,您可能需要像GHDL这样的模拟器 。 但是,要调试您的模拟,有几种不同的方法: 经典打印语句 - 只需在程序代码中混合writeline(output,[...]) 。 看到这个你好世界的例子。 如果你刚刚开始,那么添加打印语句将是非常宝贵的。 对于我所做的大部分模拟调试(这是我工作的一部分),我几乎完成了基于我们在设计和测试平台中构建的打印语句的所有调试。 它只用于最终调试,或者用于更难以处理的问题,我使用下一个调试方法。 “倾销”模拟( 对于GHDL请参阅此页面和此 页面 )。 ... -
vhdl代码(for循环)(vhdl code (for loop))[2022-05-16]
首先,您应该知道如何在vhdl中定义数组。 让我为你定义一个数组。 type array_of_integer array(19 downto 0) of integer; signal A : array_of_integer :=(others => 0); signal max : integer; -- Now above is the array in vhdl of integers all are initialized to value 0. A(0 ... -
好吧,测试平台确实存在问题。 我和同学一起在实验室里练习了。 谢谢你的帮助,无论如何,非常感谢。 ps我所做的只是在最底部的测试平台中改变了一些时序值,当负载和换档位发生时,我让它工作。 Well it was indeed something in the testbench that was giving issues. I worked it out in the lab with fellow classmates. Thank You for your help anyways it is muc ...