加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Executors没有运行所有线程.

发布时间:2020-12-15 04:16:01 所属栏目:Java 来源:网络整理
导读:我是 Java新手,我正在尝试这个.我有方法,我希望并行运行该方法.我希望有10个线程调用该方法并获得他们的结果. 我正在使用Callable和Executors.我正在创建线程池: ExecutorService executor = Executors.newFixedThreadPool(10); 当我这样做时: executor.in
我是 Java新手,我正在尝试这个.我有方法,我希望并行运行该方法.我希望有10个线程调用该方法并获得他们的结果.

我正在使用Callable和Executors.我正在创建线程池:

ExecutorService executor = Executors.newFixedThreadPool(10);

当我这样做时:

executor.invokeAll(taskList);

在10个线程中,仅从轮询中获取了1个线程.我只打印了这个:

The current thread is pool-1-thread-1

但我希望应该有10个类似的println语句.

这是完整的代码:

import java.util.concurrent.Callable;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.Future;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;

public class Parallel
{
    public static void main(String args[])
    {
        Learning l = new Learning();
        l.message = "1st Object";
        l.testThread();
    }
}

//this class deals with threads
class Learning
{

    public String message;

    public void setMessage(String message)
    {
        this.message = message;
    }

    //contains the code where
    public void testThread()
    {

       //create a callable for each method
       Callable<String> callable1 = new Callable<String>()
       {
          @Override
          public String call() throws Exception
          {
             System.out.println("The current thread is " + Thread.currentThread().getName());
             return method1();
             // return null;
          }
       };


       //add to a list
       List<Callable<String>> taskList = new ArrayList<Callable<String>>();
       taskList.add(callable1);

       //create a pool executor with 10 threads
       ExecutorService executor = Executors.newFixedThreadPool(10);


       try
       {
         List<Future<String>> futureList = executor.invokeAll(taskList);

       }
       catch (InterruptedException ie)
       {
       }
    }

    //put your code here!
    private String method1()
    {
        return Thread.currentThread().getName();
    }
}

我在这里错过了什么吗?

解决方法

您的ExecutorService可以运行10个线程.但你只提交了一个帖子.将testThread方法更改为这样.

// contains the code where
public void testThread() {
    // add to a list
    List<Callable<String>> taskList = new ArrayList<Callable<String>>();
    Callable<String> callable1=null;
    for (int i = 0; i < 10; i++) {
        // create a callable for each method
        callable1 = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("The current thread is " + Thread.currentThread().getName());
                return method1();
                // return null;
            }
        };
        taskList.add(callable1);
    }

    // create a pool executor with 10 threads
    ExecutorService executor = Executors.newFixedThreadPool(10);

    try {
        List<Future<String>> futureList = executor.invokeAll(taskList);

    } catch (InterruptedException ie) {
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读