java - What's the dedicate way to use ExecutorService and Future? -
i have batch of items. , each item, can itemid, use id db lookup , generate transactions batch.
list<batchitem> batchitemslist; // assume list have list of items list<transaction> results = new arraylist<>(); executorservice executor = executors.newfixedthreadpool(5); list<future<transaction>> tasks = new arraylist<>(); for(batchitem item: batchitemslist) { mythread worker = new mythread(item); future<transaction> future = executor.submit(worker); tasks.add(future); } for(future<transaction> t: tasks) { results.add(t.get()); }
mythread.java
public class mythread implements callable<transaction>{ private final batchitem batchitem; public mythread(batchitem batchitem) { this.batchitem = batchitem; } @override public transaction call() throws exception { string id = batchitem.getid(); // sql lookups.... transaction t = new transaction(sql lookup result values...); return t; } }
question 1: need create mythread object? if have 15k items, means i'll create 15k mythread objects in memory... what's dedicate way solve problem?
question 2 need check if future done? if so, how should handle isdone()?
Comments
Post a Comment