Tracking streams when they recursively

I am currently working on some code for the course. I cannot post the code, but I am allowed to talk about some high level concepts that I am struggling with and getting. Basically the code is recursive DFS on undirected graph which I have to convert to parallel program. My professor already pointed out that I should create my streams in a recursive DFS method and then attach them to another method. Basically, I'm having trouble thinking about how I should keep track of the threads I create, so I can join all of them in a different method. I think an array of Threads, but I'm not sure how to add each new thread to the array, or even in the right direction.

+2


a source to share


3 answers


Another way to achieve this is with BlockingQueue

and ThreadPoolExecutor

. You can continuously add new streams to BlockingQueue

, count how many you added, and then terminate ThreadPoolExecutor

when you're done.



private ThreadPoolExecutor pool;
private BlockingQueue<Runnable> queue;
...
this.pool = new ThreadPoolExecutor(10, 10, new Long(1000), 
                TimeUnit.MILLISECONDS, this.queue);
...

//new thread created and added to the queue
requestedTasks++

if (requestedTasks == this.pool.getCompletedTaskCount() && this.queue.isEmpty()) {
  this.pool.shutdown();
}

      

+2


a source


Sounds right, you need a list of topics. Since you will be accessing the list / array from multiple threads, you can use one of the list classes with a thread, or use the "addThread (Thread newCreatedThread)" method, which is synchronized. Hope this helps!



0


a source


You can create a new ThreadGroup object in your main application thread and then make all your unresolved members of its threads. You just have to be careful with the oddball semantics of the enumerate method in ThreadGroup when you try to return them. (read the javadoc carefully!)

What is the purpose of joining them? Just find out if everything is done? It might be helpful to know about implementing the Delegate pattern in Java with interfaces.

0


a source







All Articles