• Post Reply Bookmark Topic Watch Topic
  • New Topic

ThreadPoolExecutor  RSS feed

 
mariqn nenchev
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all
i need to develop the following:
PriceProcessor that starts in separate thread and generate prices for List of items that must be send to another module. The price generation is long task, so i separated it to another class that implements Runnable . What i want is to make the PriceProcessor in while(isRunning) loop to start list of Price runnables (the list is created by algorithm that says how many runnables to be created) and while the runnables from the list are not finished the processor must not generate new prices for the items(the prices must be generated as frequently as possible). So i want to use This ThreadPoolExecutor but the problem is that i don't know how to tell him that he must run list of runnables and wait all of them to finish before to execute the new list of runnables:
Example:
PriceProcessor pp = new PriceProcessor();
pp.start();
Items items = new Items(List<Integer> itemids);
// here i make request to the price processor to begin generating prices for that items
pp.process(items); // this will be called 1 time a day to update the items list with new items. The price processor will use the current items if new didn't arrived. This is like self generating task
.................

public class PriceProcessor() extends Thread {
private BlockingQueue<Items> blockingQueue = new ArrayBlockingQueue<Items>(1);

public SelfTaskGenMultiThreadedProcessor(TaskThreadPooledExecutor executor) {
this.executor = executor;
}

@Override
public void process(Runnable target) {
try {
blockingQueue.put(target);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public void run() {
isRunning = true;
while (isRunning) {
try {
items = blockingQueue.take();
// all runnables must be executed before the next loop
List<Runnable> runnables = createRunnables(items);
if (runnables != null) {
synchronized (runnables) {
for(runnables loop)
executor.executeTask(runnable);
}
}
//wait for all runnables from above to finish and then simulate new request for the same items if new once didn't arrived(the queue has size 1 so if durring the upper loop new items didn't arrived // the current onces are used)
synchronized (blockingQueue) {
if (blockingQueue.isEmpty()) {
blockingQueue.put(items);
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}

So the main question is how to make the ThreadPoolExecutor to wait all runnables from the list to finish and then go on and create new list of runnables. NB: The number of runnables depends on the items size and type.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use a java.util.concurrent.ExecutorCompletionService. If you look at the API I think you will see an example that does just what you want.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!