ogzylz
ogzylz

Reputation: 1395

concurrent programming in java

I have 2 threads running in paralel. The run function of the threads is as follows

public void run(){
    Runtime rt = Runtime.getRuntime();
    Process s;
    try {
      s = rt.exec("cd /.../somefolder/"+i+"/");
      closeStream(s); // this closes process s

      s = rt.exec("sh adapMs.sh");
      closeStream(s); // this closes process s
    } ...
}

adapMs.sh creates some folders, files .. under the current directory which is specified by the line

  s = rt.exec("cd /.../somefolder/"+i+"/");

For example thread1 uses the directory 1. While thread1 uses the directory 1, another thread2 executes the line

  s = rt.exec("cd /.../somefolder/"+i+"/");

which is directory 2.

Does this cause thread1 to create its new files under directory 2 or it creates it folders, files under directory 1 anyway?

in other words, does thread 2 cause to change thread1's current directory?

Upvotes: 2

Views: 296

Answers (3)

Stephen C
Stephen C

Reputation: 718758

in other words, does thread 2 cause to change thread1's current directory?

What happens in the execution of an external process is entirely up to the operating system, not Java.

If the OS's implementation of the "cd" command was such that one process could change the current directory of another process, then that would happen. If not, then it wouldn't.

No mainstream operating system that I've heard of allows one process to change another processes current directory ... so in practice the answer to your question is "No". But the most technically correct answer would be "Check your operating system / shell documentation".

Upvotes: 1

Nikita Rybak
Nikita Rybak

Reputation: 68006

Not sure whether your solution is workable, but this is clearly the intended way to solve your problem in Java:

rt.exec("sh adapMs.sh", null, new File("/.../somefolder/" + i + "/"));

edit removed 'cd' and added file

Upvotes: 1

Starkey
Starkey

Reputation: 9781

Each exec runs in its own thread and its own environment. If thread 1 is in directory 1, it will stay in directory 1 (and be uneffected by thread 2).

Upvotes: 1

Related Questions