From 63e2de56ac6b638ced43d9e48d75a82213e52f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20GRILLON?= Date: Wed, 14 Nov 2018 11:02:01 +0100 Subject: [PATCH] inputStreamToSelectableChannel thread safe --- Expect.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Expect.java b/Expect.java index 70ba0b9..d291ac8 100644 --- a/Expect.java +++ b/Expect.java @@ -7,6 +7,7 @@ import java.nio.channels.Pipe; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; +import java.nio.channels.Pipe.SinkChannel; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -86,10 +87,18 @@ private static Pipe.SourceChannel inputStreamToSelectableChannel( final InputStream input) throws IOException { Pipe pipe = Pipe.open(); pipe.source().configureBlocking(false); - final OutputStream out = Channels.newOutputStream(pipe.sink()); - Thread piping = new Thread(new Runnable() { + + class OneShotTask implements Runnable { + + SinkChannel sink; + InputStream input; + OneShotTask(SinkChannel s, InputStream in) { sink = s; input=in; } + @Override public void run() { + + final OutputStream out = Channels.newOutputStream(pipe.sink()); + //LOG byte[] buffer = new byte[1024]; try { @@ -115,7 +124,8 @@ public void run() { } } } - }); + } + Thread piping = new Thread(new OneShotTask(pipe.sink(), input)); piping.setName("Piping InputStream to SelectableChannel Thread"); piping.setDaemon(true); piping.start();