java - How to execute this paralell task in Java8 -
i'm new in java concurrency, ask best way perform action this:
i have static method matches sub image within image. looks that:
public static point match(final bufferedimage subimage, final bufferedimage image)
the method returns null if nothing matched, else returns point of match.
now have 40 different sub images 1 (big) image, want match in parallel. each second new (big) image in need search 40 smaller images on , on again. need return values of each call to match method @ end of match task in main task, can analyze it. furthermore need use many cpu cores possible task.
how can accomplish that? have read lot executorservice, task, runnable , on. examples show how print on console in paralles. confused way should go in scenario: how pass values , how results? how should layout of class(es) like? have no idea way go if create 40 tasks second (it take time setup task, right?)
code great explain :)
use completionservice
, more executorcompletionservice
.
class matcher { executorservice threadpool = executors.newcachedthreadpool(); private list<bufferedimage> subimages; // populate public static point match(bufferedimage subimage, bufferedimage image) { // implementation } public list<point> match(bufferedimage image) { completionservice<point> completionservice = new executorcompletionservice(threadpool); int size = subimages.size(); list<point> results = new arraylist<>(size); (bufferedimage subimage: subimages) { completionservice.submit(()->match(subimage, image)); } (int = 0; < size; i++) { point point = completionservice.take().get(); if (point != null) { results.add(point); } } return results; } }
if want use cpu, want change executorservice
executors.newworkstealingpool()
. careful though!
Comments
Post a Comment