Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java创建一个连续线程数组_Java_Multithreading_Concurrency - Fatal编程技术网

Java创建一个连续线程数组

Java创建一个连续线程数组,java,multithreading,concurrency,Java,Multithreading,Concurrency,我的任务是让多个线程执行永无止境的任务/创建一个永无止境的线程,该线程将一次又一次地运行相同的“void”方法,直到触发器完成它 这是第一次尝试 现在尝试线程,但仍然无法获得所需的结果。 这是: boolean IsRunning = true; Integer noThreads = 5; ThreadGroup tg = new ThreadGroup("A"); Thread[] listThreads = new Thread[noThreads]; tg.enumerate(list

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

这是第一次尝试

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

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);
    }
boolean IsRunning=true;
整数=5;
螺纹组tg=新螺纹组(“A”);
线程[]listThreads=新线程[noThreads];
枚举(listThreads);
@RequestMapping(value=“/start\u new”,method=RequestMethod.POST)
公共可调用StartNewTask(@RequestBody LaunchSend sendobj)引发IOException、InterruptedException{
Runnable Runnable=()->{
同时(正在运行){
MyVoid();
}
};
for(int i=0;i“可调用结果”;
}
@异步的
void MyVoid(){
Globals.getInstance().increment();
System.out.println(Thread.currentThread().getName()+“iteration#”+Globals.getInstance().Iterator);
}

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

因此IsRunning被设置为false somewhere,您的“可调用结果”应该是什么?不管怎样,你都应该使用ExecutorService,就像你在上面提到的帖子中所做的那样。此外,如果您想在某个时间点停止任务,它们不会永远运行。因此IsRunning在某个地方设置为false-否“您无论如何都应该使用ExecutorService,就像您在上述帖子中所做的那样。此外,如果您想在某个时间点停止任务,它们不会永远运行。”如果我使用线程数组,我也可以阻止它们,至少我需要以我首先需要的方式运行它们……1)使用执行器服务2)IsRunning被设置为false 3)IDK,但Spring的默认线程实现可能会随着请求的完成而终止调用的线程?