Use of the future interface and callable Interface

Source: Internet
Author: User
  1. Import
    Java. util. arraylist;
  2. Import
    Java. util. List;
  3. Import
    Java. util. Concurrent. callable;
  4. Import
    Java. util. Concurrent. executionexception;
  5. Import
    Java. util. Concurrent. executorservice;
  6. Import
    Java. util. Concurrent. executors;
  7. Import
    Java. util. Concurrent. Future;
  8. Import
    Java. util. Concurrent. futuretask;
  9. Public
     
    Class
    Concurrentcalculator {
  10. Private
    Executorservice exec;
  11. Private
     
    Int
    Cpucorenumber;
  12. Private
    List <future <long> tasks =
    New
    Arraylist <future <long> ();
  13. // Internal class

  14. Class
    Sumcalculator
    Implements
    Callable <long> {
    // The part used for calculation in the thread, which is equivalent to the run () method

  15. Private
     
    Int
    [] Numbers;
  16. Private
     
    Int
    Start;
  17. Private
     
    Int
    End;
  18. Public
    Sumcalculator (
    Final
     
    Int
    [] Numbers,
    Int
    Start,
    Int
    End ){
  19. This
    . Numbers = numbers;
  20. This
    . Start = start;
  21. This
    . End = end;
  22. }
  23. Public
    Long call ()
    Throws
    Exception {
  24. Long sum = 0l;
  25. For
    (
    Int
    I = start; I <end; I ++ ){
  26. Sum + = numbers [I];
  27. }
  28. Return
    SUM;
  29. }
  30. }
  31. Public
    Concurrentcalculator (){
  32. Cpucorenumber = runtime. getruntime (). availableprocessors ();
  33. Exec = executors. newfixedthreadpool (cpucorenumber );
  34. }
  35. Public
    Long sum (
    Final
     
    Int
    [] Numbers ){
  36. // Split tasks based on the number of CPU cores, create futuretask, and submit it to executor

  37. For
    (
    Int
    I =
    0
    ; I <cpucorenumber; I ++ ){
  38. Int
    Increment = numbers. Length/cpucorenumber +
    1
    ;
  39. Int
    Start = increment * I;
  40. Int
    End = increment * I + increment;
  41. If
    (End> numbers. length)
  42. End = numbers. length;
  43. Sumcalculator subcalc = new
    Sumcalculator (numbers, start, end );
  44. Futuretask <long> task = new
    Futuretask <long> (subcalc );
    // Thread

  45. Tasks. Add (task );
  46. If
    (! Exec. isshutdown ()){
  47. Exec. Submit (task); // execution thread

  48. }
  49. }
  50. Return
    Getresult ();
  51. }
  52. /**
     
  53. * Iterate each task to get the part and return the sum.
     
  54. *
     
  55. * @ Return
     
  56. */

  57. Public
    Long getresult (){
  58. Long result = 0l;
  59. For
    (Future <long> task: tasks ){
  60. Try
    {
  61. // Blocking if the calculation is not completed

  62. Long subsum = task. Get (); // obtain the computing result of each thread.

  63. Result + = subsum;
  64. } Catch
    (Interruptedexception e ){
  65. E. printstacktrace ();
  66. } Catch
    (Executionexception e ){
  67. E. printstacktrace ();
  68. }
  69. }
  70. Return
    Result;
  71. }
  72. Public
     
    Void
    Close (){
  73. Exec. Shutdown ();
  74. }
  75. Public
     
    Static
     
    Void
    Main (string [] ARGs)
  76. {
  77. Int
    [] Numbers =
    New
     
    Int
    [] {
    1
    ,
    2
    ,
    3
    ,
    4
    ,
    5
    ,
    6
    ,
    7
    ,
    8
    ,
    10
    ,
    11
    };
  78. Concurrentcalculator calc = new
    Concurrentcalculator ();
  79. Long sum = Calc. sum (numbers );
  80. System. Out. println (SUM );
  81. Calc. Close ();
  82. }
  83. }

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.