有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程Java创建一个连续线程数组

我的任务是让多个线程执行永无止境的任务/创建一个永无止境的线程,该线程将一次又一次地运行相同的“void”方法,直到触发器完成它

这是第一次尝试 Execute a continious task via ThreadPoolExecutor

现在尝试线程,但仍然无法获得所需的结果。 这是:

boolean IsRunning = true;
Integer noThreads = 5;
ThreadGroup tg = new ThreadGroup("A");
Thread[] listThreads = new Thread[noThreads];
tg.enumerate(listThreads);

@RequestMapping(value = "/start_new", method = RequestMethod.POST)
public Callable<String> StartNewTask(@RequestBody LaunchSend sendobj) throws IOException, InterruptedException {

    Runnable runnable = () -> {
        while(IsRunning) {
            MyVoid();
       }
    };

    for (int i = 0; i < noThreads; i++) {
        listThreads[i] = new Thread(runnable);
        listThreads[i].start();
    }

    return () -> "Callable result";
}

 @Async
    void MyVoid(){
       Globals.getInstance().increment();
       System.out.println(Thread.currentThread().getName()+" iteration # "+ Globals.getInstance().Iterator);
    }

方法“MyVoid”处于while(true){}循环中,因此它应该连续执行,但它没有。每个线程只执行一次


共 (0) 个答案