简体   繁体   English

给定数量的线程之间的java多线程拆分计算

[英]java multithreading split calculation between a given number of threads

I have simple method that calculates pow of each number in array and returns these numbers in new array ... My question is how I can split this calculation to given number of threads and thereby speed up the execution of the method 我有一个简单的方法来计算数组中每个数字的pow并在新数组中返回这些数字...我的问题是我如何将这种计算拆分为给定数量的线程,从而加快该方法的执行

public class ExtClass{
    public static long pow(long a, int b) {
        if (b == 0)        return 1;
        if (b == 1)        return a;
        if ((b & 1) == 0)  return     pow (a * a, b/2);
        else               return a * pow(a * a, b/2);

    }

    public static long[] val(long[] a, int p) {
        long[] result_array = new long[a.length];

        for (int i = 0; i < a.length - 1; i++) {
            result_array[i] = pow(a[i], p);
        }

        return result_array;
    }

PS I'm totally noob in java and need your help plz =) PS我完全是java的菜鸟,需要你的帮助plz =)

On a high level: 从高层次看:

  1. You create a callable class, that has the implementation of the, pow method. 您创建一个可调用类,该类具有pow方法的实现。
  2. You create a executor based on the max available processors that you seemed to have figured out. 您可以根据您似乎已经确定的最大可用处理器来创建执行程序。
  3. You create the callables and submit them to the executor. 您创建可调用对象并将其提交给执行者。
  4. Using the Future's returned you collate the results 使用Future的返回结果,您可以整理结果

These links should get you started: https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6 http://www.journaldev.com/1090/java-callable-future-example 这些链接应该使您入门: https : //blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6 http://www.journaldev.com/1090/java-callable-future-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM