I have some class. For example:
public class Data {
private String name;
public Data(String url) {
// There is download something from the Internet and set field "name".
}
public String getName() {
return name;
}
}
In some method I need to initialize array of objects Data.
ArrayList<Data> list = new ArrayList<Data>;
for(int i=0; i<max; i++) {
list.add(new Data("http://localhost/" + String.valueOf(i)));
}
But it is to long. I wanna do this:
final ArrayList<Data> list = new ArrayList<Data>;
for(int i=0; i<max; i++) {
final int tmp = i;
new Thread() {
public void run() {
list.add(new Data("http://localhost/" + String.valueOf(tmp)));
}
}.start();
}
But the main thread ends sooner than the others and variable list is empty. What should I do? Help pls :)
UP. That is not too fast to download some data from the Internet that's why I've created several threads.
"http://localhost/" + String.valueOf(tmp)is the same as"http://localhost/" + tmp