2

If my java program wants to execute several perl scripts at various times using threads. Should each thread have its own copy of a ProcessBuilder and Process objects ?

I was thinking that the threads could all share the ProcessBuilder but, i'm not too sure about that.

assylias
  • 321,522
  • 82
  • 660
  • 783
rage
  • 1,777
  • 5
  • 25
  • 36
  • Related to http://stackoverflow.com/questions/17298717/can-a-thread-in-java-monitor-processes-that-it-starts – Gray Jun 25 '13 at 17:53

1 Answers1

2
  • If all the parameters of your ProcessBuilder are always the same (i.e. you always call the same script with the same arguments), you can use the same ProcessBuilder in all your threads and only need to make sure that it is properly published to those threads.

    Typically, if you start your threads after having created and setup the ProcessBuilder you will be fine.

  • If however different threads need to make changes to the ProcessBuilder (or if you make changes to the builder after the threads have been started), you will need to synchronize those changes - cf the javadoc:

    Note that this class is not synchronized. If multiple threads access a ProcessBuilder instance concurrently, and at least one of the threads modifies one of the attributes structurally, it must be synchronized externally.

    In that case, it would probably be easier to use one instance per thread.

assylias
  • 321,522
  • 82
  • 660
  • 783