Programme one:
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.future;public class Largsumwithcallable {static int Threadcounts =10;//number of threads used static long sum=0; public static void Main (String []args) throws Interruptedexception, executionexception{Executorservice exec= Executors.newfixedthreadpool (threadcounts); List<callable<long>> calllist=new arraylist<callable<long>> (); list<integer> list = new arraylist<integer> (); for (int j = 0; J <= 1000000;j++) {List.add (j); } int len=list.size ()/threadcounts;//The average number of split list//list no more threads (rarely exists) if (len==0) {threadcounts=l Ist.size ();//Use a thread to process an element in the list len=list.size ()/threadcounts;//re-average split list} for (int I=0;i<threadcoun ts;i++) { Final list<integer> sublist; if (i==threadcounts-1) {sublist=list.sublist (I*len,list.size ()); }else{sublist=list.sublist (I*len, len* (i+1) >list.size ()? List.size (): len* (i+1)); }//Using an anonymous inner class implementation Calllist.add (new callable<long> () {public Long call () throws Exception {Long subsum=0l; for (Integer i:sublist) {subsum+=i; } System.out.println ("Assigned to Thread:" +thread.currentthread (). GetName () + "That part of the list of integers is: \tsubsum:" +subsum); return subsum; } }); } list<future<long>> Futurelist=exec.invokeall (calllist); for (future<long> future:futurelist) {sum+=future.get (); } exec.shutdown (); SYSTEM.OUT.PRINTLN (sum); } }
Scenario Two:
Import Java.util.arraylist;import Java.util.list;import Java.util.concurrent.brokenbarrierexception;import Java.util.concurrent.cyclicbarrier;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class Largelistintegersum {Private long sum;//holds integers and private cyclicbarrier barrier;//Barrier set point (Synchronizer) private list<integer> list;//integer collection List private int threadcounts;//number of threads used public Largelistintegersum (list<integer> list,int threadcounts) {this.list=list; this.threadcounts=threadcounts; }/** * Gets all the integers in the list and * @return */public long getintegersum () {Executorservice Exec=exe Cutors.newfixedthreadpool (threadcounts); int len=list.size ()/threadcounts;//average number of split in list//list no more threads (rarely exists) if (len==0) {threadcounts =list.size ();//Use a thread to process an element in the list len=list.size ()/threadcounts;//re-average split list} barrier=new C YclicbarrieR (threadcounts+1); for (int i=0;i<threadcounts;i++) {//Create thread task if (i==threadcounts-1) {///The last thread takes on all remaining elements of the calculation Exec.execute (New Subintegersumtask (List.sublist (I*len,list.size ())); }else{Exec.execute (New Subintegersumtask (List.sublist (I*len, len* (i+1) >list.size ()? List.size (): len* (i +1))); }} try {barrier.await ();//key, so that the thread waits at the barrier until all the threads reach the barrier} catch (interruptedexcept Ion e) {System.out.println (Thread.CurrentThread (). GetName () + ": Interrupted"); } catch (Brokenbarrierexception e) {System.out.println (Thread.CurrentThread (). GetName () + ": Brokenbarrier"); } exec.shutdown (); return sum; }/** * Split calculates the list integer and the thread task * */public class Subintegersumtask implements runnable{p Rivate list<integer> sublist; Public Subintegersumtask (LIST<INTEGER> sublist) {this.sublist=sublist; } public void Run () {long subsum=0l; for (Integer i:sublist) {subsum + = i; } synchronized (Largelistintegersum.this) {//Synchronize sum+=subsum on Largelistintegersum object; } try {barrier.await ();//key, so that the thread waits at the barrier until all the threads reach the barrier} catch (Interrupte Dexception e) {System.out.println (Thread.CurrentThread (). GetName () + ": Interrupted"); } catch (Brokenbarrierexception e) {System.out.println (Thread.CurrentThread (). GetName () + ": Brokenbarrier" ); } System.out.println ("Assigned to Thread:" +thread.currentthread (). GetName () + "That part of the list of integers is: \tsubsum:" +subsum); }} public static void Main (string[] args) {list<integer> List = new arraylist< Integer> (); int threadcounts = number of threads used by 10;// for (int i = 1; I <= 1000000; i++) {list.add (i); } long start= System.currenttimemillis (); Largelistintegersum countlistintegersum=new largelistintegersum (list,threadcounts); Long Sum=countlistintegersum.getintegersum (); System.out.println ("The and of all integers in the list is:" +sum); Long end= system.currenttimemillis (); System.out.println (End-start); } }
Multithreading Concurrency fast processing data