Java Fork Join Forkjoinpool Usage Example

Source: Internet
Author: User

This example is to decompose a large array of sums of large tasks into small tasks that are summed in small areas, and then add together these small tasks to the result.

Technology: JDK8.0, Recursivetask technology in Java fork-join mode, override Compute ().

[Java]View Plaincopy print?
  1. /**
  2. * Author:bigtree
  3. * This example is the large task of summing up a large array into small tasks that sum in small sums, and then adding up the sum of these small tasks is the result of the calculation.
  4. Technology
  5. * Recursivetask technology in Java fork-join mode, override Compute ().
  6. */
  7. Import java.util.*;
  8. Import java.util.concurrent.*;
  9. Class Caltask extends recursivetask<long>{
  10. private static final int THRESHOLD = 1000; //how Many numbers one thread can calculate
  11. private long data[];
  12. private int start;
  13. private int end;
  14. Public Caltask (long[] inputdata,int start, int end) {
  15. Data=inputdata;
  16. This.start=start;
  17. This.end=end;
  18. }
  19. @Override
  20. protected Long Compute () {
  21. Long sumresult=0l;
  22. if ((End-start) <= THRESHOLD) {
  23. For (int index=start; index<end;index++) {
  24. Sumresult + = Data[index];
  25. }
  26. }
  27. else { //parallel computing
  28. int step= (end-start)/threshold;
  29. if (((End-start)%threshold) >0)
  30. step+=1;
  31. arraylist<caltask> tasks = new arraylist<> ();
  32. int Pos=start;
  33. int lastposition;
  34. For (int i=0;i<step;i++) {
  35. Lastposition=pos+threshold;
  36. if (lastposition>end)
  37. Lastposition=end;
  38. Caltask onetask= New Caltask (data,pos,lastposition);
  39. Pos=lastposition;
  40. Tasks.add (OneTask);
  41. Onetask.fork ();
  42. }
  43. For (Caltask mtask:tasks) {
  44. Sumresult + = Mtask.join ();
  45. }
  46. }
  47. return sumresult;
  48. }
  49. }
  50. public class Forkjoincompute {
  51. public static void Forkjoinshow () {
  52. long data[] = new long[20001];
  53. For (long i=0;i<data.length;i++) {
  54. data[(int) i]= i + 1;
  55. }
  56. Forkjoinpool mypool= Forkjoinpool.commonpool ();
  57. future<long> myfuture = Mypool.submit ( new Caltask (data,0,data.length));
  58. try{
  59. long Result=myfuture.get ();
  60. System.out.println ("Forkjoincompute (): Computed final result=" +result);
  61. } catch (Interruptedexception e) {
  62. E.printstacktrace ();
  63. } catch (Executionexception e) {
  64. //TODO auto-generated catch block
  65. E.printstacktrace ();
  66. }
  67. Mypool.shutdown ();
  68. }
  69. }
[Java]View Plaincopy print?


Output Result:

Forkjoincompute (): Computed final result=200030001

Java Fork Join Forkjoinpool Usage Example

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.