私はJavaが初めてで、演習としてスレッド化されたライブラリを書きたいと思っています。このように動作します。
メイン スレッドでは、いくつかのジョブ (文字列として) がジョブ キューに追加され、ワーカー スレッドがジョブを終了すると、ジョブが終了キューに追加されます。メイン スレッドは、終了したキューから結果を取得します。すべてのジョブが完了すると、メイン スレッドはワーカーに停止するように通知します。これまでに書いたいくつかのコードは次のとおりです。
public List<int> get() {
WorkerThread[] threads = new WorkerThread[numThreads];
LinkedList<int> results = new LinkedList<>();
int workCount = 0;
for (int i = 0; i < numThreads; i++) {
threads[i] = new WorkerThread();
threads[i].start();
}
// reader is a BufferedReader
while ((String line = reader.readLine()) != null) {
// put string to job queue
workCount++
}
while(workCount) {
//result = get result from finished queue, and add it to results LinkedList
workCount--;
}
for (int i = 0; i < numThreads; i++) {
threads[i].canStop(); // this sets a private variable that makes infinite while loop to stop
threads[i].join();
}
return results;
}
しかし、これに使用する Queue 実装の種類について混乱しています。ドキュメントには、11 種類の Queue 実装が示されています。