如何处理具有多个扫描程序(多个资源)的多个线程?



我正在尝试使用四个扫描仪从四个不同的来源获取数据。我确实意识到我需要使用线程。但这是错误消息:

P.S = 在使用线程之前,文件的路径很好。(我正在使用一个文件,路径还可以。

null null null null java.lang.ArrayIndexOutOfBoundsException: 41705 at getText.getCities(getText.java:132(at getText$1.run(getText.java:23(at java.lang.Thread.run(Unknown Source(java.lang.ArrayIndexOutOfBoundsException: 41705

at getText.getNames(getText.java:112(at getText$2.run(getText.java:30(at java.lang.Thread.run(Unknown Source(









代码本身

公共类 getText {

static int ln = 41705;
static String [] icaos = new String[ln];
static String [] iatas = new String[ln];
static String [] names = new String[ln];
static String [] cities = new String[ln];
public final Runnable typeA;
public final Runnable typeB;
public final Runnable typeC;
public final Runnable typeD;
public getText() {
typeA = new Runnable() {
public void run() {
getText.this.getCities();
}
};

typeB = new Runnable() {
public void run() {
getText.this.getNames();
}
};
typeC = new Runnable() {
public void run() {
getText.this.getIcao();
}
};
typeD = new Runnable() {
public void run() {
getText.this.getIata();
}
};
}
public static void main(String[] args) {
// TODO Auto-generated method stub
getText x = new getText();
new Thread(x.typeA).start();
new Thread(x.typeB).start();
new Thread(x.typeC).start();
new Thread(x.typeD).start();
System.out.println(icaos[32541]);
System.out.println(iatas[32541]); 
System.out.println(names[32541]); 
System.out.println(cities[32541]); 
}
public void getIcao () {
try {
int i=0;
InputStream icao_stream = new FileInputStream("src/icao.txt");
Scanner icao_s = new Scanner(icao_stream);
icao_s.useDelimiter(",");
while(icao_s.hasNext()) {
icaos[i] = icao_s.next();
i++;
}
icao_s.close();
icao_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getIata() {
try {
int i=0;
InputStream iata_stream = new FileInputStream("src/iata.txt");
Scanner iata_s = new Scanner(iata_stream);
iata_s.useDelimiter(",");
while(iata_s.hasNext()) {
iatas[i] = iata_s.next();
i++;
}
iata_s.close();
iata_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getNames() {
try {
int i=0;
InputStream names_stream = new FileInputStream("src/names.txt");
Scanner names_s = new Scanner(names_stream);
names_s.useDelimiter(",");
while(names_s.hasNext()) {
names[i] = names_s.next();
i++;
}
names_s.close();
names_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getCities() {
try {
int i=0;
InputStream cities_stream = new FileInputStream("src/cities.txt");
Scanner cities_s = new Scanner(cities_stream);
cities_s.useDelimiter(",");
while(cities_s.hasNext()) {
cities[i] = cities_s.next();
i++;
}
cities_s.close();
cities_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}

所以现在的问题是你已经启动了你的线程,但控件立即返回到主线程,而你的其他线程到那时还没有完成工作。您需要的是等待这些线程完成工作,然后检查结果。有多种方法可以实现这一点。一个简单的方法是将main更改为:

public static void main(String[] args) {
getText x = new getText();
List<Thread> threads = new ArrayList<>();
threads.add(new Thread(x.typeA));
threads.add(new Thread(x.typeB));
threads.add(new Thread(x.typeC));
threads.add(new Thread(x.typeD));
threads.forEach(t -> t.start());
threads.forEach(t -> {
try {
//this makes the main thread to wait for thread "t" to finish
t.join();
} catch (InterruptedException e) {
// log or throw or anything you need to do
}
});
System.out.println(icaos[32541]);
System.out.println(iatas[32541]);
System.out.println(names[32541]);
System.out.println(cities[32541]);
}

这不是最优化的解决方案,而是最简单的解决方案。其他更好的选择是使用CyclicBarrierExecutorService等。

最新更新