我在多個線程中運行我的threadPool
每個線程讀取一個巨大的文件並從列表中返回此文件中的數據。線程池中的多個線程在同一列表中寫入數據
代碼如下所示:
class Writer{
ArrayList finalListWhereDataWillBeWritten = new Array<Integer>()
for(query q : allQueries){ //all the read queries to read file
threadPool.submit(new GetDataFromFile(fileName,filePath));
}//all the read queries have been submitted.
}
現在我知道下面的代碼段會出現一些地方在我的代碼,但我不知道在哪裏把它。 因爲如果我將它放在for循環中的submit()
之後,它不會添加它,因爲每個文件都非常大,可能尚未完成處理。
synchronized(finalListWhereDataWillBeWritten){
//process the data obtained from single file and add it to target list
finalListWhereDataWillBeWritten.addAll(dataFromSingleThread);
}
因此,誰能告訴我,在哪裏我把這個代碼塊,我需要什麼其他的事情,以確保如此DONOT發生的臨界區問題。
class GetDataFromFile implements Runnable<List<Integer>>{
private String fileName;
private String filePath;
public List<Integer> run(){
//code for streaming the file fileName
return dataObtainedFromThisFile;
}
}
?我需要使用wait()
/notifyAll()
方法在我的代碼因爲我只在線程從文件中讀取數據平行並將它們放在一個共享列表
這是一個半烤問題。請在問題結束前發佈所有相關代碼。 – CKing
您還需要什麼信息?我可以提供 – veer
對於初學者,您展示的兩個代碼片段之間發生了什麼。什麼是'dataFromSingleThread'?爲什麼不發佈實際的代碼片段呢? – CKing