views:

97

answers:

3

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?

+1  A: 

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).

Starkey
I tought so but was not sure. thnx...
ogzylz
+1  A: 

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

Nikita Rybak
your solution works for sure. I abandon my soln'.s = rt.exec("sh adapMs.sh", null, new File("/somefolder/")); works.
ogzylz
+1  A: 

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".

Stephen C