【问题标题】:Threadpool with callable带有可调用的线程池
【发布时间】:2018-03-24 22:07:17
【问题描述】:

我对可调用的 ThreadPool 方法感到震惊。我想在数组中找到大数以及它出现的频率,所以我做了所有事情,但它显示错误。任何人都可以帮助我。谢谢。

import java.util.concurrent.Callable;
public class CallableMethod im``plements Callable<Integer>{

    //@SuppressWarnings("unused")
    private int[] num;

    public CallableMethod(int [] num){
        this.num = num;
    }

    public Integer call() throws Exception{

        int n = num[0];
        int frequency = 0;

        for(int i=1; i< num.length; i++)
        {

               if(n < num[i]){
                        n = num[i];
               }
        }

         for(int i = 1; i< num.length; i++){
           if (n == num[i]){
                frequency++;
            }
        }

        //System.out.println("Largest Number is : " + num);
        //System.out.println("frequency of occurence : " + frequency);
        return frequency;
    }
}

上面是我的 callabe() 代码和

import java.util.concurrent.*;
import java.util.*;

class ThreadPoolMethod {
    // static ExecutorService pool = Executors.newFixedThreadPool(2);

    public static void main(String[] args) {
        ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
        int number[] = { 32, 43, 145, 53, 25, 98, 54, 32, 65, 63, 145, 98, 43, 23, 25, 98, 100, 102, 105, 123, 145,
                122, 123, 11, 12, 1, 0, 123, 145, 145 };
        ArrayList<Future<Integer>> future = new ArrayList<Future<Integer>>();
        for (int j = 0; j < number.length; j++) {
            Future<Integer> f = pool.submit(new CallableMethod(number));
            future.add(f);
        }
        // create array to store results
        int result[] = new int[number.length];
        for (int j = 0; j < result.length; j++) {
            try {
                Future<Integer> f = future.get(j);
                result[j] = f.get();
            } catch (InterruptedException e) {
            } catch (ExecutionException e) {
            };
        }
        System.out.println("The Large Number in array is: " + n);
        System.out.println("The : " + frequency);
        pool.shutdown();
        for(int x : result)
            System.out.print(x);
    }
} 

这是我的线程池。请让我震惊。我无法将可调用工作调用到 ThreadPool 方法中。请帮助我

【问题讨论】:

    标签: java threadpool callable


    【解决方案1】:

    尝试在带有 Streams、CompletableFuture、ForkJoinPool 的 java 8 上使用此示例

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ForkJoinPool;
    import java.util.stream.Collectors;
    
    
    public class DemoController {
    
        public static void main(String[] args) {
            ForkJoinPool forkJoinPool = new ForkJoinPool(2);
            int number[] = {32, 43, 145, 53, 25, 98, 54, 32, 65, 63, 145, 98, 43, 23, 25, 98, 100, 102, 105, 123, 145,
                    122, 123, 11, 12, 1, 0, 123, 145, 145};
            List<CompletableFuture<Integer>> future = new ArrayList<>();
            for (int j = 0; j < number.length; j++) {
                CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> func(number), forkJoinPool);
                future.add(f);
            }
            List<Integer> result = future.stream().map(f -> {
                try {
                    return f.get();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }).collect(Collectors.toList());
            forkJoinPool.shutdown();
            result.forEach(System.out::println);
        }
    
        private static Integer func(int num[]) {
            int n = num[0];
            int frequency = 0;
            for (int i = 1; i < num.length; i++) {
                if (n < num[i]) {
                    n = num[i];
                }
            }
            for (int i = 1; i < num.length; i++) {
                if (n == num[i]) {
                    frequency++;
                }
            }
            System.out.println("Largest Number is : " + n);
            System.out.println("frequency of occurence : " + frequency);
            return frequency;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-17
      • 2018-06-28
      • 2011-10-20
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      相关资源
      最近更新 更多