Create threads in java to run in background

后端 未结 3 1413
误落风尘
误落风尘 2020-11-30 01:57

I want to spawn a Java thread from my main java program and that thread should execute separately without interfering with the main program. Here is how it should be:

<
3条回答
  •  半阙折子戏
    2020-11-30 02:24

    And if you like to do it the Java 8 way, you can do it as simple as this:

    public class Java8Thread {
    
        public static void main(String[] args) {
            System.out.println("Main thread");
            new Thread(this::myBackgroundTask).start();
        }
    
        private void myBackgroundTask() {
            System.out.println("Inner Thread");
        }
    }
    

提交回复
热议问题