I have code that includes a line which runs a shell script (a .bat file on AIX). The output from this .bat file is incorrect unless the code is delayed before the line that runs the shell script. For example, the 'sleep' method or a statement that writes to a log needs to be applied first to slow down the code sufficiently. These are inefficient work-arounds though and will cause problems if there are a number of concurrent users. The .bat file and other files that it refers to are created by the code prior to when the code runs the .bat file. I'm therefore wondering whether a solution might be to have all the prior code broken up into a series of 'synchronized' methods that might resolve any multi-threading issues.
Can you show us any fragments of the code that runs before the shell? I'm imagining writing to files and closing them, and it seems that should all be synchronous and complete before you get to the next instruction, unless AIX buffers output and takes a while to close.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I guess this would happen because the opening of the bat file is different process. I mean once the bat file is opened whatever things it does is independent of java. So JVM has no idea that you would like your .bat to complete before moving forward. You probably need some kind of inter process communication.
I find it a bit difficult to help without knowing exactly you're trying to do; for example: why a delay is necessary after creating the script to executing it, why use a script rather than Java code (is there something OS dependent), when you say concurrency issues do you mean in the same JVM or across multiple JVMs?
If the concurrency is an issue in the same JVM, some form of concurrent lock (synchronized methods/blocks and/or use of java.util.concurrent) might help you. Also, if you create a Process for executing the script, you can use the waitFor() method on that Process to block the Java program until the script completes.
If the problem is across multiple JVMs (where synchronisation won't work), you can try file locks: first obtain a java.nio.channels.FileChannel (e.g. via java.io.FileInputStream), then invoke its lock() or tryLock() methods. If the Java implementation on AIX supports file locks, that will prevent other instances of your program from accessing the same file - hence the file becomes safe between different processes in the same OS.
Is that of any help?
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )
posted 12 years ago
I added a new entry to this forum with code details. It turns out this was a permissions issue. The code was applying a 'chmod' to the .bat file, but apparently this process did not always complete on time. Adding the line below with the 'waitFor' method seemed to resolve this.
Process chmodChild = Runtime.getRuntime().exec(chmodCommand); chmodChild.waitFor();
Thanks for your responses.
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book