简体   繁体   English

Java中的线程。 在新线程中创建每个图形,循环不起作用

[英]Threading in Java. Creating each figure in new thread, loop not working

I want to create a separate thread for each of my created rectangles. 我想为我创建的每个矩形创建一个单独的线程。 I need to pass arguments to run the thread, and it's not allowed. 我需要传递参数来运行线程,这是不允许的。 I can't figure out how to do this. 我不知道该怎么做。 This is what I already wrote: 这是我已经写的:

    int number_of_cubes = 10;
    Rect[] r1 = new Rect[number_of_cubes];
    for(int i=0; i <number_of_cubes;i++){
        Thread myThread = new Thread(new Runnable()
        {
            public void run(Rect[] r1,int i){
                Random rn = new Random();
                Random rand = new Random();
                float r = rand.nextFloat();
                float g = rand.nextFloat();
                float b = rand.nextFloat();
                Color randomColor = new Color(r, g, b);
                r1[i] = new Rect(rn.nextInt(600), rn.nextInt(400), 15, 15, randomColor);
            }

        });
    }

As for your immediate question, use 至于您的直接问题,请使用

final Rect[] r1 = new Rect[number_of_cubes];
for (int i = 0; i < number_of_cubes; i++) {
  final int targetIndex = i;
  new Thread(new Runnable() { public void run() {
    ...
    r1[targetIndex] = ...
  }}).start();

And a few notes: 和一些注意事项:

  • the overhead of thread creation is large enough for this idiom to start making sense only if you have a substantial amount of work for it. 线程创建的开销足够大,只有在您需要大量工作的情况下,这种习惯用法才有意义。 Say, at least 10,000 rectangles; 假设至少有10,000个矩形;

  • you are redundantly creating two Random instances. 您正在冗余地创建两个Random实例。 Use just one per thread; 每个线程仅使用一个;

  • watch out for visibility issues: you may use the rectangle array only after all threads have finished ( join on each thread from the main method); 注意可见性的问题:你可以使用所有线程都完成后才会矩形阵列( join从每个线程的main方法);

  • you'll experience performance gain only with a moderate number of threads, usually equal to the number of available CPU cores; 仅在中等数量的线程(通常等于可用的CPU内核数量)下,您才能体验到性能提升;

  • a much better approach would be to use an Executor Service. 更好的方法是使用执行器服务。

Do this, It will create a function that runs the thread you want with the parameters. 这样做,它将创建一个函数,该函数运行带有参数的所需线程。 Then, in the for loop you can call it however you would like: 然后,在for循环中可以调用它,但是您希望:

nt number_of_cubes = 10;
Rect[] r1 = new Rect[number_of_cubes];

for(int i=0; i <number_of_cubes;i++){
    //call the function here if you want
}
public void runThread(final Rect[] r1,final int i){
    new Thread(new Runnable(){
        @Override
        public void run(){
            Random rn = new Random();
            Random rand = new Random();
            float r = rand.nextFloat();
            float g = rand.nextFloat();
            float b = rand.nextFloat();
            Color randomColor = new Color(r, g, b);
            r1[i] = new Rect(rn.nextInt(600), rn.nextInt(400), 15, 15, randomColor);
        }

    }).start();
}   

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

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