简体   繁体   English

Java:线程分为块数组-执行器服务

[英]Java: threading divided into blocks array - executor service

I am creating a program to calculate values of two arrays in steps of simulation (they are initialized from the beginning, I did not put it here).我正在创建一个程序来在模拟步骤中计算两个 arrays 的值(它们从头开始初始化,我没有放在这里)。 I would like to do it with threads and ExecutorService.我想用线程和 ExecutorService 来做。 I divided arrays into blocks and I want values of these blocks to be calculated by threads, one block = one thread.我将 arrays 分成块,我希望这些块的值由线程计算,一个块 = 一个线程。 These two arrays - X and Y - take values from each other (as you can see in run()), I want X to be calculated first and Y after that, so I made two separate runnables:这两个 arrays - X 和 Y - 相互取值(如您在 run() 中所见),我希望先计算 X,然后计算 Y,所以我制作了两个单独的可运行文件:

public static class CountX implements Runnable {
    private int start;
    private int end;
    private CountDownLatch cdl;
    public CountX(int s, int e, CountDownLatch c) {
        this.start = s;
        this.end = e;
        this.cdl = c;
    }
    public void run() {
        for (int i = start + 1; i < end - 1; i++) {
            x[i] = x[i] - (y[i-1] - 2 * y[i] + y[i+1]) + y[i];
        }
        cdl.countDown();            
    }
}

And same for CountY. CountY 也是如此。 I would like to give to it the information where the start and end of value for every block is.我想向它提供每个块的值的开始和结束位置的信息。

This is, in a short, how my main looks like and this is the main problem of mine:简而言之,这是我的主要外观,这是我的主要问题:

int NN = 400; //length of X and Y
int threads = 8;
int block_size = (int) NN/threads;

final ExecutorService executor_X = Executors.newFixedThreadPool(threads); 
final ExecutorService executor_Y = Executors.newFixedThreadPool(threads);
CountDownLatch cdl = new CountDownLatch(threads);
    
CountX[] runnables_X = new CountX[threads];
CountY[] runnables_Y = new CountY[threads];

for (int r = 0; r < threads; r++) {
        runnables_X[r] = new CountX((r*block_size), ((r+1)*block_size), cdl); 
}
for (int r = 0; r < threads; r++) {
        runnables_Y[r] = new CountY((r*block_size), ((r+1)*block_size), cdl);
}


int sim_steps = 4000;
for(int m = 0; m < sim_steps; m++) {
    for (int e = 0; e < threads; e++) {
        executor_X.execute(runnables_X[e]);
    }
    for (int e = 0; e < threads; e++) {
        executor_Y.execute(runnables_Y[e]);
    }
}
executor_X.shutdown();
executor_Y.shutdown();

I get wrong values of arrays X and Y from this program, because I also did it without threads.我从这个程序中得到了错误的 arrays X 和 Y 值,因为我也没有线程。 Is CountDownLatch necessary here?这里需要 CountDownLatch 吗? Am I supposed to do for loop of runnables_X[r] = new CountX((r*block_size), ((r+1)*block_size), cdl);我应该为runnables_X[r] = new CountX((r*block_size), ((r+1)*block_size), cdl);循环做吗? in every m (sim_step) loop?在每个 m (sim_step) 循环中? Or maybe I should use ExecutorService in a different way?或者也许我应该以不同的方式使用 ExecutorService? I tried many options but the results are still wrong.我尝试了很多选项,但结果仍然是错误的。 Thank you in advance!先感谢您!

Your approach is one I probably wouldn't take for this task.您的方法是我可能不会为这项任务采取的方法。

You can work with references and Runnables, but in your case a Callable might be the better choice.您可以使用引用和 Runnables,但在您的情况下, Callable可能是更好的选择。 With a Callable, you just give it the array and let it calculate a partial value, if possible and await the Futures .使用 Callable,您只需给它数组并让它计算部分值,如果可能的话并等待Futures For me, it's not really clear what you actually want to calculate though, thus I am taking a blind guess here.对我来说,虽然不清楚你真正想要计算什么,所以我在这里盲目猜测。

You don't need a CountDownLatch nor two ExecutorServices - one EXS is enough.您不需要CountDownLatch也不需要两个ExecutorServices - 一个 EXS 就足够了。

If you really want to use a Runnable for this, you should implement some sort of synchronization, either with a concurrent list, Atomic variables, volatile or a lock.如果您真的想为此使用Runnable ,您应该使用并发列表、原子变量、 volatile或锁来实现某种同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM