• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

'synchronized' Methods

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

I'd appreciate any ideas on this. Thanks.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

it would be interesting to know why the output is wrong, i.e. what causes these errors. It seems to difficult to provide a meaningful answer to your question in the abstract.

Cheers,
Oliver
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic