爪哇 - 灌装与循环线程的ArrayList [英] Java - Filling an ArrayList of Threads with loop

查看:80
本文介绍了爪哇 - 灌装与循环线程的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题大概是pretty容易回答,但我只是不明白这一点。
我减少了我的问题,直到这code的小片留在为了找到这个问题的原产地:
我想,以填补线程的ArrayList一个循环。

this question is probably pretty easy to answer but I just don't get it. I reduced my problem until this little piece of code was left in order to find the "origin" of this problem: I'm trying to fill an ArrayList of Threads with a loop.

public static int u=0;

public void test(){
    while (u <10) {
        synchronized(threadList){
            threadList.add(u, new Thread(){                
                @Override public void run(){                    
                    System.out.println("Thread at Index: " + u);                     
                } 
            });
        }
        u++;            
    }

    threadList.get(2).start();    
}

随着最后一行我希望通过从索引'2'线程测试上面的循环。
我期待控制台显示索引主题:2,而是这是图所示:
线程指数:10
不管我在写一个整数获得(INT) - 的方法,我收到指数10

With the last line I wanted to test the loop above by starting the thread at Index '2'. I'm expecting the console to show "Thread at Index: 2" but instead this is shown: "Thread at Index: 10" No matter which integer I write in the ".get(int)"-method, I receive the index '10'.

这是为什么?而如何解决这一问题?

Why is that? And how to fix this?

该线程的创建似乎工作...所以是整数'U'问题?

The creation of the threads seems to work...so is the integer 'u' the problem?

我AP preciate任何形式的帮助!
在此先感谢!

I appreciate any kind of help! Thanks in advance!

推荐答案

当您引用 U 运行方法

@Override public void run(){                    
    System.out.println("Thread at Index: " + u);                     
} 

U 的当前值被检索。在你的循环结束和线程运行时,该值 10

The current value of u is retrieved. At the end of your loop and when the thread runs, that value is 10.

请尝试以下

public static int u = 0;

public void test(){
    while (u <10) {
        synchronized(threadList){
            threadList.add(u, new Thread(){     
                int i = u;

                @Override public void run(){                    
                    System.out.println("Thread at Index: " + i);                     
                } 
            });
        }
        u++;            
    }

    threadList.get(2).start();    
}

在您的匿名类,你实例字段 I 的值设置为 U 的时候调用构造函数并执行的run()打印时该值。

In your anonymous Thread class, you're setting an instance field i to the value u has when the constructor is called and printing that value when the run() is executed.

您参考 U 在尚未执行,一个线程上下文。当的run()方法最终被调用时,程序会计算变量 U ,但在这一点上程序的执行,其价值将是10。相反,如果你做到以上,变量 I 将在这个确切的时刻持有的价值。这是因为当新的得到启动和执行,现场发生的初始​​化和 U 被评估的时候了。

You reference u in a context that hasn't been executed yet, a thread. When the run() method eventually gets called, the program will evaluate the variable u, but at that point in the execution of the program, its value will be 10. If, instead, you do the above, the variable i will hold the value at that exact moment. This is because when new Thread gets started and executed, field initialization occurs and u is evaluated right away.

这篇关于爪哇 - 灌装与循环线程的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆