How to run a thread separate from main thread in Java?

前端 未结 5 779
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 05:52

The goal is to be able to invoke execution of a separate thread from within the main class.

Some context: I have a program that must ru

5条回答
  •  遇见更好的自我
    2021-02-04 06:53

    Create a separate thread that executes your external program:

    class MyRunner implements Runnable{
      public void run(){
         Runtime.exec("your cmd")
      }
    }
    

    then start the thread in your main():

    MyRunner myRunner = new MyRunner(); 
    Thread myThread = new Thread(myRunner);
    myThread.start();
    

    This way your main program will continue running, while your background thread will start an external process and exit when this program exits.

提交回复
热议问题