我在一个数组中创建了一个Thread of Threads,我希望这个数组中包含的每个对象生成一个线程,它基本上是一个运动。每个物体应该以不同的方向移动,但它们都朝着相同的方向移动。 到目前为止我得到了这个:
for (int i=0; i<network.mobiles.size(); i++) {
final int j = i ;
threadPool.submit(new Runnable() {
public void run() {
int value = 10 ;
for (int z=0; z<value; z++){
value++;
int x = mobnod[j].getLocationOnScreen().x;
int y = mobnod[j].getLocationOnScreen().y;
double r = Math.random();
if (r < 0.25) mobnod[j].getLocationOnScreen().x--;
else if (r < 0.50) mobnod[j].getLocationOnScreen().x++;
else if (r < 0.75) mobnod[j].getLocationOnScreen().y--;
else if (r < 1.00) mobnod[j].getLocationOnScreen().y++;
mobnod[j].setLocation(x, y);
network.mobiles.get(j).findClosestAP(network.nodes);
network.setConnections();
try {
threadPool.awaitTermination(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
// once you've submitted your last job to the service it should be shut down
threadPool.shutdown();
// wait for the threads to finish if necessary
}
你能帮我吗?
答案 0 :(得分:0)
您将项目的位置存储在本地x和y变量中,然后使用随机数更改项目的位置,然后将项目的位置设置回本地存储的原始x和y。也许这就是问题?