怎么在Java中使用线程计算数组和?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
代码一:
五个线程交替累加计算数组之和,这种方法其实不如单线程直接累加快,因为交替累加需要前一个线程计算的结果。
package test; import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors; public class FiveThreadCount { private int count=0; private int[] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28}; private int j=0; //定义一个任务,关键点所在 private class MyThread extends Thread{ @Override public void run() { super.run(); while(j<arr.length) { synchronized (MyThread.class) { if(j>=arr.length){ return; } count+=arr[j++]; try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); } } } } //方法一 public void test1(){ for(int i=0;i<5;i++){ new MyThread().start(); } try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(count); } //方法二 public void test2(){ Thread myThread=new MyThread(); for(int i=0;i<5;i++){ new Thread(myThread).start(); } try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(count); } //方法一的线程池实现版 public void test3(){ ExecutorService service=Executors.newCachedThreadPool(); for(int i=0;i<5;i++){ service.execute(new MyThread()); } try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(count); } //方法二的线程池实现版 public void test4(){ ExecutorService service=Executors.newCachedThreadPool(); Thread myThread=new MyThread(); for(int i=0;i<5;i++){ service.execute(myThread); } try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(count); } }
上边代码中,用到了sleep方法的原因,sleep(100)是为了让其他线程有时间执行任务,如果不sleep的话,有可能一个线程就全部执行完了。 最后的sleep(10000)是为了等所有线程执行完后,打印最后的计算结果。
代码二:
将数组分为5等分,让每个线程计算自己负责的那份,并发计算,最后汇总结果。这种方式比代码一速度会快些。因为线程独立计算,不依赖其他线程的结果。最后几个线程将总数累加即可。
方式一:
使用Callable,FutureTask方式,来实现代码:
package test; 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; public class FiveThreadCount2 { private int[] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28}; private int total=0; public void test() throws InterruptedException, ExecutionException{ ExecutorService service=Executors.newFixedThreadPool(5); int length=arr.length; for(int j=0;j<length;j+=(length/5)){ FutureTask<Integer> task; if( (j+(length/5))<length){ task=new FutureTask<Integer>(new MyCallable(arr, j, j+(length/5))); }else{ task=new FutureTask<Integer>(new MyCallable(arr, j, length)); } service.execute(task); total+=task.get(); } service.shutdown(); System.out.println(total); } public class MyCallable implements Callable<Integer>{ int[] arr; int startIndex; int endIndex; public MyCallable(int[] arr,int startIndex,int endIndex){ this.arr=arr; this.startIndex=startIndex; this.endIndex=endIndex; } @Override public Integer call() throws Exception { int sum=0; for(int i=startIndex;i<endIndex;i++){ sum+=arr[i]; } System.out.println(Thread.currentThread().getName()); return sum; } } }
这个方式有一个缺点,看似5个线程异步执行,其实是顺序执行,因为 task.get是要等待线程执行完毕才会执行下边的代码。所以效率不会高,可能换种写法可以解决这个问题,这里就不深入研究。
方式二:
通过java工具类CountDownLatch实现并发计算
package test; import java.util.concurrent.CountDownLatch; public class FiveThreadCount3 { private int[] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28}; private int total=0; public void test() throws InterruptedException{ int length=arr.length; CountDownLatch latch=new CountDownLatch(length%5==0?5:6); System.out.println(length); for(int j=0;j<length;j+=(length/5)){ MyThread task; if( (j+(length/5))<=length){ task=new MyThread(arr, j, j+(length/5), latch); }else{ task=new MyThread(arr, j, length, latch); } new Thread(task).start(); } latch.await(); System.out.println(total); } private class MyThread implements Runnable{ int[] arr; int startIndex; int endIndex; CountDownLatch latch; public MyThread(int[] arr,int startIndex,int endIndex,CountDownLatch latch){ this.arr=arr; this.startIndex=startIndex; this.endIndex=endIndex; this.latch=latch; } @Override public void run() { int sum=0; for(int i=startIndex;i<endIndex;i++){ sum+=arr[i]; } synchronized (MyThread.class) { total+=sum; } System.out.println(Thread.currentThread().getName()); latch.countDown(); } }}
对于CountDownLatch不熟悉的可以搜索下用法。
方式三:
通过java工具类 CyclicBarrier实现并发计算。
package test; import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier; public class FiveThreadCount1 { private int[] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28}; private int total=0; public void test() throws InterruptedException, BrokenBarrierException{ int length=arr.length; CyclicBarrier barrier=new CyclicBarrier((length%5==0?5:6)+1); System.out.println(length); for(int j=0;j<length;j+=(length/5)){ MyThread task; if( (j+(length/5))<=length){ task=new MyThread(arr, j, j+(length/5), barrier); }else{ task=new MyThread(arr, j, length, barrier); } new Thread(task).start(); } barrier.await(); System.out.println(total); } private class MyThread implements Runnable{ int[] arr; int startIndex; int endIndex; CyclicBarrier barrier; public MyThread(int[] arr,int startIndex,int endIndex,CyclicBarrier barrier){ this.arr=arr; this.startIndex=startIndex; this.endIndex=endIndex; this.barrier=barrier; } @Override public void run() { int sum=0; for(int i=startIndex;i<endIndex;i++){ sum+=arr[i]; } synchronized (MyThread.class) { total+=sum; } try { System.out.println(Thread.currentThread().getName()); barrier.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BrokenBarrierException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
Java是什么
Java是一门面向对象编程语言,可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序。
关于怎么在Java中使用线程计算数组和问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网行业资讯频道了解更多相关知识。