亲宝软件园·资讯

展开

Java 线程数组之和 Java使用5个线程计算数组之和

安迪爸爸 人气:0
想了解Java使用5个线程计算数组之和的相关内容吗,安迪爸爸在本文为您仔细讲解Java 线程数组之和的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,线程数组之和,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();
   }
  }
  
 }
}

总结

总体来说,代码二的方式二、三,效率会高一点。以上代码都是通过main方法调用示例代码的test方法,输出结果到控制台。 

加载全部内容

相关教程
猜你喜欢
用户评论