Reputation: 1837
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.
Upvotes: 2
Views: 99
Reputation: 328598
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.
Upvotes: 2