【发布时间】:2013-07-19 22:51:48
【问题描述】:
几分钟前我已经提出了另一个接近这个问题的问题,并且有很好的答案,但这不是我想要的,所以我试图更清楚一点。
假设我有一个类中的线程列表:
class Network {
private List<Thread> tArray = new ArrayList<Thread>();
private List<ObjectInputStream> input = new ArrayList<ObjectInputStream>();
private void aMethod() {
for(int i = 0; i < 10; i++) {
Runnable r = new Runnable() {
public void run() {
try {
String received = (String) input.get(****).readObject(); // I don't know what to put here instead of the ****
showReceived(received); // random method in Network class
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
tArray.add(new Thread(r));
tArray.get(i).start();
}
}
}
我应该放什么来代替 ** ? 例如,tArray 列表的第一个线程只能访问输入列表的第一个输入。
编辑:假设我的输入列表已经有 10 个元素
【问题讨论】:
-
也许this 会帮助你
标签: java arrays multithreading list