2

I want to start the execution of new Thread in separate command window without affecting the execution of main Thread.

I have something like this:

public static void main(String args[]){
  NewThread n = new NewThread();
  Thread t = new Thread(n);
  t.start(); //Here I want to display the execution of t in separate command prompt.

}

class NewThread implements Runnable{
 public void run(){
  //....
 }
}

Can I do it using Runtime.getRuntime().exec() ? Please Help..Thanks.

Winn
  • 413
  • 2
  • 6
  • 17
  • possible duplicate of [Java: How to run thread separately from main program/class?](http://stackoverflow.com/questions/4005350/java-how-to-run-thread-separately-from-main-program-class) – Jayram Oct 23 '13 at 06:17
  • opening a new command window would be OS specific. Are you ok with that? If so then what OS are you using? – Mateusz Dymczyk Oct 23 '13 at 06:17
  • What do you mean by "without affecting the execution of main Thread"? Are you trying to launch two processes? – David Schwartz Oct 23 '13 at 06:19
  • "without affecting the execution of main Thread" in the sense that my main program should run separately in one command prompt and the new thread should run in another. I mean, I should be able to see the execution in two separate command prompt. – Winn Oct 23 '13 at 07:01

3 Answers3

1

Use Swing and create a window with text area inside, like in java-swing-programming-buffered. This way you need not to launch external processes.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • Thanks..That could be one approach. But sorry I've limitation, Can't use GUI to display the output. – Winn Oct 23 '13 at 17:19
0

Launch /bin/sh as a separate process using Runtime.exec. If you want to communicate with that child process, use ProcessBuilder. Read answers to java-process-with-input-output-stream.

Community
  • 1
  • 1
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • But How shall I display its output in another terminal? – Winn Oct 23 '13 at 10:12
  • 1
    hmm bin/sh is not sufficient... you have to launch `gnome-terminal -e command` where command is a program which communicates with the parent process some way. Looks like `gnome-terminal` does not provide access to its `stdin` to the commands it starts. – Alexei Kaigorodov Oct 23 '13 at 13:07
  • I think java has limitation here. It should provide the ease to execute threads separately. May Be I will try this one. Lets see. Thanks Alexei. – Winn Oct 23 '13 at 17:22
-1

You can use the setDaemon() method to change the Thread daemon properties.

Grigoriev Nick
  • 1,099
  • 8
  • 24