Package concurrent; import Java. util. arraylist; import Java. util. list; import Java. util. concurrent. callable; import Java. util. concurrent. executionexception; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. futuretask; // use ten threads to calculate an array and class taskwithresult implements callable <integer> {private integer [] ints; Public taskwithresult (integer [] ints ){ This. ints = ints;} private int sumfromarray () {int result = 0; For (int A: This. ints) {result + = A;} return result ;}@ overridepublic integer call () throws exception {return sumfromarray ();}} public class extends allel1 {public static integer [] segarray (INT start, int end, int [] array) {list <integer> result = new arraylist <integer> (); for (INT I = start; I <end; I ++) {result. add (array [I]);} integer [] ary = r Esult. toarray (new integer [result. size ()]); Return ary;} public static int getsum (INT [] array) {int result = 0; For (INT I = 0; I <array. length; I ++) {result + = array [I];} return result;} public static void main (string [] ARGs) throws interruptedexception, executionexception {long start1 = system. currenttimemillis (); executorservice exec = executors. newfixedthreadpool (10); int [] array = {300,800, 89,390, 892,938 4, 909, 1,343,583 9, 939,43, 355,323, 32, 55, 3, 3, 43, 5, 5, 45,555,554,554,555,545,555,553, 35,232 2, 332,323 2, 433,344,524,245,524,656 5, 526}; List <futuretask <integer> tasks = new arraylist <futuretask <integer> (); For (INT I = 0; I <10; I ++) {int incret = array. length/10; int start = I * incret; int end = (I + 1) * incret; If (end> array. length) End = array. length; integer [] PRT = segarray (START, end, array); taskwithresult calbtask = new taskwithresult (PRT); futuretask <integer> task = new futuretask <integer> (calbtask); tasks. add (task); If (! Exec. isshutdown () {exec. submit (task) ;}} int result = 0; For (futuretask <integer> task: tasks) {int partrst = task. get (); Result + = partrst;} exec. shutdown (); long end1 = system. currenttimemillis (); system. out. println ("parallel computing time:" + (end1-start1); system. out. println ("Parallel Computing Result:" + result); long start2 = system. currenttimemillis (); int result2 = getsum (array); long end2 = system. currenttimemillis (); system. out. println ("parallel computing time:" + (end2-start2); system. out. println ("results calculated separately:" + result2 );}}
Package concurrent; import Java. util. arraylist; import Java. util. list; import Java. util. concurrent. callable; import Java. util. concurrent. completionservice; import Java. util. concurrent. executionexception; import Java. util. concurrent. executorcompletionservice; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; // use ten threads to calculate an array and class taskwithresult1 implements callable <integer> {Private integer [] ints; Public taskwithresult1 (integer [] ints) {This. ints = ints;} private int sumfromarray () {int result = 0; For (int A: This. ints) {result + = A;} return result ;}@ overridepublic integer call () throws exception {return sumfromarray ();}} public class extends allel2 {public static integer [] segarray (INT start, int end, int [] array) {list <integer> result = new arraylist <integer> (); for (INT I = Start; I <end; I ++) {result. add (array [I]);} integer [] ary = result. toarray (new integer [result. size ()]); Return ary;} public static int getsum (INT [] array) {int result = 0; For (INT I = 0; I <array. length; I ++) {result + = array [I];} return result;} public static void main (string [] ARGs) throws interruptedexception, executionexception {executorservice exec = executors. newfixedthreadpool (10); completions Ervice <integer> cplsvc = new executorcompletionservice <integer> (EXEC); int [] array = {300,800, 89,390,892,938 4, 909, 1,343,583 9, 939,43, 355,323, 32, 55, 3, 3, 43, 5, 5, 45,555,554,554,555,545,555,553, 35,232 2, 332,323 2, 433,344,524,245,524,656 5, 526}; For (INT I = 0; I <10; I ++) {int incret = array. length/10; int start = I * incret; int end = (I + 1) * incret; If (end> array. leng Th) End = array. length; integer [] PRT = segarray (START, end, array); taskwithresult1 calbtask = new taskwithresult1 (PRT); If (! Exec. isshutdown () {cplsvc. submit (calbtask) ;}} int result = 0; For (INT I = 0; I <10; I ++) {int partrst = cplsvc. take (). get (); Result + = partrst;} exec. shutdown (); system. out. println ("Parallel Computing Result:" + result); int result2 = getsum (array); system. out. println ("results calculated separately:" + result2 );}}