hotnwwp
hotnwwp

Reputation: 51

Can I use pipedoutputstream for multi threads?

Can i use one pipedoutputstream for multi threads and connect it to one pipedinputstream, then get all the output from the multi threads?

Following is the code snippet, what i want is the modify and delete thread's output can be used for sync thread, but delete thread's output is missed. List P4Thread's run() method in the end.

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);
            }
        }
}

Upvotes: 3

Views: 644

Answers (1)

user207421
user207421

Reputation: 310913

Yes you can, as long as you take care of synchronizing your multi-threaded writes so they don't interleave.

Upvotes: 2

Related Questions