s4553711
7/31/2017 - 9:33 AM

StreamRead.java

# - https://dzone.com/articles/concurrency-java-futures-and-kotlin-coroutines-1
# - https://stackoverflow.com/questions/41412359/java8-idiomatic-way-to-process-a-streamcallable-in-parallel-delivering
public class StreamRead {
    public static void main(String[] args) {
        int nThread = args.length > 1 ? Integer.valueOf(args[1]) : 2;
        ExecutorService pool = Executors.newFixedThreadPool(nThread);
        
        int sampleNum = 1;
        try (Stream<String> s = Files.lines(Paths.get(args[0]))) {
            s.filter(q -> !q.startsWith("\"#")).parallel().map(line -> {
                return new Callable<MegaRecord>() {
                    @Override
                    public MegaRecord call() throws Exception {
                        MegaRecord rec = new MegaRecord(sampleNum, line);
                        return rec;
                    }
                };
            }).map(callable -> pool.submit(callable)).map(future -> {
                try {
                    return future.get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                return 1;
            }).forEach(rec -> {
                MegaRecord ret = (MegaRecord) rec;
                //System.out.println(ret.getChr()+" "+ret.getPos());
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}