d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Execservice / Threadpool
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Apr 10 2014 08:11pm
TL;DR version - Is there a way to recursively create workers, then once you reach a certain number (or depth in the tree), have them perform some operation on the nodes below them? such as a search for a node?) I'm not sure I implemented mine correctly and so lost atm

I'm trying to come up with a way to implement a tree search looking for a specific node.

My idea is that I can recursively create callables using the executorservice for the first two levels of the tree. After this point each worker will perform a search of their portion of the remaining tree and search for the node.

Is there a way to cap the number of workers created, while allowing workers already created to perform a search of their given subtrees?

I've tried using a fixedthread.pool but haven't I'm having trouble creating more than the first two sets of workers from the starting node of the tree.

Desperately in search of any help or tips, thanks!

Pretty much what my code looks like atm...

Code
public class Array[Node] findNode
public static ExecutorService ex = Executors.newFixedThreadPool(nThreads);

public findK(){
MyTask runnableObj = new MyTask(tree);
Future<Array<Node>> callFuture = ex.submit(runnableObj);

ex.shutdown();
try {
// Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
ex.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e1) {
e1.printStackTrace();
}

try {
solution = callFuture.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return solution;
}

class MyTask implements Callable<Array[Node]>{

public MyTask(Tree tree) {
super(tree);
// TODO Auto-generated constructor stub
}

public List<Direction> call() throws Exception {

....Trying to call this recursively so that I have a fixed number of workers searching subsections of the tree
..but not sure how to implement it

}
}




This post was edited by lopelurag on Apr 10 2014 08:19pm
Member
Posts: 5,385
Joined: Mar 28 2014
Gold: 1,014.04
Apr 11 2014 06:24am
:thumbsup:
Member
Posts: 2,095
Joined: Apr 15 2003
Gold: 100.08
Apr 17 2014 05:32pm
If I got your question right, you might want to have a look into CountDownLatch class
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll