From dd42f88d76dba35ae47113b321655a53a1c05316 Mon Sep 17 00:00:00 2001 From: Marko Živković Date: Mon, 22 Dec 2014 20:02:59 +0000 Subject: CommandLineExecutor for USB Device Detector is now thread safe (and thus working consistently across different machines) git-svn-id: https://svn.code.sf.net/p/xlogo4schools/svn/trunk@21 3b0d7934-f7ef-4143-9606-b51f2e2281fd --- .../usbdrivedectector/process/CommandLineExecutor.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/logo/src/net/samuelcampos/usbdrivedectector/process/CommandLineExecutor.java b/logo/src/net/samuelcampos/usbdrivedectector/process/CommandLineExecutor.java index c6bfdcd..72820f3 100644 --- a/logo/src/net/samuelcampos/usbdrivedectector/process/CommandLineExecutor.java +++ b/logo/src/net/samuelcampos/usbdrivedectector/process/CommandLineExecutor.java @@ -12,6 +12,9 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * Changed by Marko Zivkovic: Introduced ThreadLocal variable for input. + * Access is now thread safe, working on each thread individually (and thus requires to call all methods on each thread separately) */ package net.samuelcampos.usbdrivedectector.process; @@ -32,7 +35,7 @@ public class CommandLineExecutor implements Closeable { private static final Logger logger = LogManager.getLogger(CommandLineExecutor.class); private Process process = null; - private BufferedReader input = null; + private final ThreadLocal input = new ThreadLocal(); public void executeCommand(String command) throws IOException { if (logger.isTraceEnabled()) { @@ -41,14 +44,14 @@ public class CommandLineExecutor implements Closeable { process = Runtime.getRuntime().exec(command); - input = new BufferedReader(new InputStreamReader(process.getInputStream())); + input.set(new BufferedReader(new InputStreamReader(process.getInputStream()))); } public String readOutputLine() throws IOException { if(input == null) throw new IllegalStateException("You need to call 'executeCommand' method first"); - String outputLine = input.readLine(); + String outputLine = input.get().readLine(); if(outputLine != null) return outputLine.trim(); @@ -60,7 +63,7 @@ public class CommandLineExecutor implements Closeable { public void close() throws IOException { if (input != null) { try { - input.close(); + input.get().close(); } catch (IOException e) { logger.error(e.getMessage(), e); } @@ -70,7 +73,7 @@ public class CommandLineExecutor implements Closeable { process.destroy(); } - input = null; + input.set(null); process = null; } -- cgit v1.2.3