开发者

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?


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜