我正在研究一種新的NLP分類器算法,並希望使其成爲多線程。我的實現執行它應該正確執行的操作,但在運行threads = 4時出現一些奇怪的異常。 這是我的方法:Java產卵幽靈線程
protected void generateCoordinatesMultiThread(Category generate, int threads){
WordMap wordMap = new WordMap();
wordOccurences = new int[semanticspace.getVectorAmount()][generate.getSize()];
tfidf = new double[semanticspace.getVectorAmount()][generate.getSize()];
ntfidf = new double[semanticspace.getVectorAmount()][generate.getSize()];
Thread[] threadGroup = new Thread[threads];
try{
System.out.println(generate.getSize()+" objects in "+threads+" threads");
for(int i=0;i<threads;i++){
int start=(generate.getSize()/threads)*i;
int end=((generate.getSize()/threads)*(i+1))-1;
if(i==threads-1){
end=generate.getSize();
}
System.out.println("Start: "+start+" end: "+end);
threadGroup[i] = new Thread(new WordMapper(this,start,end,generate,"WordMapper-"+i,wordMap));
threadGroup[i].start();
}
for(int i=0;i<threads;i++){
threadGroup[i].join();
}
System.out.println("First multi-thread step finished");
wordMap.mapSemanticSpace(semanticspace);
wordMap.print();
}catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("Starting frequency workers");
threadGroup = new Thread[threads];
try{
System.out.println(generate.getSize()+" objects in "+threads+" threads");
for(int i=0;i<threads;i++){
int start=(generate.getSize()/threads)*i;
int end=((generate.getSize()/threads)*(i+1))-1;
if(i==threads-1){
end=generate.getSize();
}
System.out.println("Start: "+start+" end: "+end);
threadGroup[i] = new Thread(new FrequencyWorker(this,start,end,generate,"FrequencyWorker-"+i,wordMap));
threadGroup[i].start();
}
for(int i=0;i<threads;i++){
threadGroup[i].join();
}
System.out.println("Second multi-thread step finished");
}catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("Starting coordinate generators");
threadGroup = new Thread[threads];
try{
System.out.println(generate.getSize()+" objects in "+threads+" threads");
for(int i=0;i<threads;i++){
int start=(generate.getSize()/threads)*i;
int end=((generate.getSize()/threads)*(i+1))-1;
if(i==threads-1){
end=generate.getSize();
}
System.out.println("Start: "+start+" end: "+end);
threadGroup[i] = new Thread(new CoordinateGenerator(this,start,end,generate,"FrequencyWorker-"+i,wordMap));
threadGroup[i].start();
}
for(int i=0;i<threads;i++){
threadGroup[i].join();
}
System.out.println("Third multi-thread step finished");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
這讓我異常:在線程
異常 「主題-5」 顯示java.lang.NullPointerException
在semanticobjects.WordMapper.run(WordMapper的.java:21)
在java.lang.Thread.run(Thread.java:695)
與9和13相同。這兩個數字在運行線程數= 4時不應該首先生成。結果計算也是正確的。我只是得到這個例外。有什麼想法嗎?
請指出whith行引發異常 - 標記'WordMapper.java:21' – Antoniossss
是什麼讓您認爲theads是虛線?如果是線程號,請注意這個數字只是一個增加的靜態int,例如,如果您重新運行該方法,數字會更大(並且線程1可能是主線程,所以應該將4個新線程命名爲Thread -2到Thread-5)。如果您希望線程在調用該方法時具有相同的名稱,請爲該構造函數提供一個名稱。 – Thomas
@SirTobiSwobi,但說實話,使用ExecutorService和Callable任務會更簡單。 – Antoniossss