Java program multithread recursion to make up for the management flaw

Source: Internet
Author: User

Java multithreading recursion in our use when we need to continue to learn, in fact, each language can be found in the source code to solve the problem. This sequential cycle is appropriate for parallelization when each iteration is independent of each other and completes the work of each iteration of Java multithreaded recursion, which is significant enough to compensate for the overhead of managing a new task.

1.public voidparallelrecursive (final executorexec,

List

2.for (Node n:nodes) {

3.exec.execute (New Runnable () {

4.public void Run () {

5.results.add (N.compute ());

6.}

7.});

8.parallelRecursive (Exec,n.getchildren (), results);

9.}

10.}

11.public Collection getparallelresults (List

12.throws interruptedexception{

13.ExecutorService Exec=executors.newcachedthreadpool ();

14.Queue resultqueue=newconcurrentlinkedqueue ();

15.parallelRecursive (Exec,nodes,resultqueue);

16.exec.shutdown ();

17.exec.awaittermination (Long.max_value,timeunit.seconds);

18.return Reslutqueue;

19.}

However, Java multithreaded recursive programs cannot handle situations where there are no scenarios, and the following programs can solve the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.