Java - 用循环填充线程的 ArrayList [英] Java - Filling an ArrayList of Threads with loop

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

问题描述

这个问题可能很容易回答,但我就是不明白.为了找到这个问题的起源",我减少了我的问题,直到留下这段代码:我正在尝试用循环填充线程的 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"无论我在.get(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?

我感谢任何形式的帮助!提前致谢!

I appreciate any kind of help! Thanks in advance!

推荐答案

当你在你的 run 方法中引用 u

When you reference u in your run method

@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();    
}

在您的匿名 Thread 类中,您将实例字段 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 将保存那个确切时刻的值.这是因为当新的 Thread 开始并执行时,会发生字段初始化,并且会立即评估 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.

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

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