gpt4 book ai didi

java - 我可以将 pipedoutputstream 用于多线程吗?

转载 作者:行者123 更新时间:2023-11-30 11:44:38 31 4
gpt4 key购买 nike

我可以为多线程使用一个 pipedoutputstream 并将其连接到一个 pipedinputstream,然后从多线程获取所有输出吗?

以下是代码片段,我想要的是修改和删除线程的输出可以用于同步线程,但是删除线程的输出丢失了。最后列出P4Thread的run()方法。

PipedInputStream input = new PipedInputStream();
PipedOutputStream output = new PipedOutputStream();

input.connect(output);

P4Thread syncthread = new P4Thread (new String[]{p4exe, "-x-", "sync", "-f"},input, out);
P4Thread modifythread = new P4Thread (new String[]{p4exe, "diff", "-se"},new ClosedInputStream(), output);
P4Thread deletethread = new P4Thread (new String[]{p4exe, "diff", "-sd"},new ClosedInputStream(), output);

try {
syncthread.start();
modifythread.run();
output.flush();

deletethread.run();
output.flush();
output.close();

syncthread .join();
} catch (InterruptedException e) {
syncthread .interrupt();
}

public void run() {
Launcher.ProcStarter ps = new Launcher.LocalLauncher(listener).launch();
ps.envs(env).stdin(input).stdout(output).cmds(cmdList);
if (workDir != null) {
ps.pwd(workDir);
}
try{
ps.join();
}catch (InterruptedException e) {
if (output != null && closePipes) {
IOUtils.closeQuietly(output);
}
//return -1;
} catch (IOException e) {
if (output != null && closePipes) {
IOUtils.closeQuietly(output);
}
} finally {
if (closePipes) {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
}
}
}

最佳答案

是的,你可以,只要你注意同步你的多线程写入,这样它们就不会交错。

关于java - 我可以将 pipedoutputstream 用于多线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10695405/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com