初めてJava先物を使用しようとしています。一部のファイルを解凍するためのクラスを設定しています。zipファイルを含むzipファイルがあるため、再帰的に解凍したい。
callable を実装する Uncompressor クラスをインスタンス化するドライブ クラスがあります。Uncompressor は解凍を開始し、別の .zip に遭遇すると、それ自体のインスタンスを作成し、それをプールに追加して続行します。
疑似コード
From DriverClass:
.
.
.
ExecutorService pool = new Executors.newFixedThreadPool(4);
Uncompressor uc = new Uncompressor(pool, compressedFile);
Collection<File> files = uc.uncompress();
for(Future <Collection<File>> f : uc.futures)
files.addAll(f.get());
// at the end of this loop, files doesnt seem to hold all of my files
そして、ここに私のアンコンプレッサークラスがあります
public class Uncompressor implements Callable<Collection<File>>
{
public Set<Future<Collection<File>>> futures = new HashSet<Future<Collection<File>>>();
File compressedFile;
public Uncompressor(ExecutorService pool, File compressedFile)
{
this.pool = pool;
this.compressedFile = compressedFile;
}
public Collection<File> call() throws Exception[
return uncompress();
}
public Collection<File> uncompress()
{
List<File> uncompressedFiles = new ArrayList<File>();
.
.Loop
.//Try to uncompress the file. If the archive entry is a zip file, do the following:
Callable<Collection<File>> callable = new Uncompressor(this.pool, archiveFileEntry);
Future f = pool.submit(callable);
futures.add(f);
//else, add files to a collection here for returning
uncompressedFiles.add(archiveFileEntry);
.EndLoop
return uncompressedFiles;
.
.
}
したがって、問題は私のDriverClassにあり、再帰的なダイビングからのすべての非圧縮ファイルを保持する必要があるファイルのコレクションには、すべてのファイルが含まれているようには見えません。Future から戻り値を取得する際に何か問題があると思います。クラスメンバー変数をfutures
定義した方法が原因ですか?
ありがとうございました