public class Gardener {
private int amountFlowerbed;
Gardener(int amountFlowerbed){
this.amountFlowerbed=amountFlowerbed;
}
Flowerbed[] flowerbeds = new Flowerbed[amountFlowerbed];
public void work() {
Watering_machine wateringMachine = new Watering_machine();
Thread[] threads = new Thread[amountFlowerbed];
for (int i=0;i<amountFlowerbed;i++) {
flowerbeds[i]=new Flowerbed(wateringMachine,i+1);
threads[i] = new Thread(flowerbeds[i]);
threads[i].start();
}
}
}
当我用amountFlowerbed初始化数组花坛时,我得到了这个例外,但是如果我用一些具体的值(例如2)做它,它可以正常工作。因此,amountFlowerbed包含相同的值。最奇怪的是线程数组用amountFlowerbed初始化