您应该使用ExecutorService并提交等待读取服务输出的任务.
package demo;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class TaskDemo {
public static void main(String[] args ) {
System.out.println("Hello");
ExecutorService threadPool = Executors.newFixedThreadPool(1);
Runnable task = () -> {
try {
//Loop to read your services's output
Thread.sleep(1000);
System.out.println("This is from Task");
} catch(InterruptedException e) {
e.printStackTrace();
}
};
threadPool.execute(task);
//Wait for the task to finish and shutdow the pool
threadPool.shutdown();
}
}