只有一个线程从大小为5的ExecutorService newFixedThreadPool运行

时间:2017-10-30 01:53:10

标签: java multithreading

我正在尝试学习ExecutorService newFixedThreadPool的行为。我将工作分配给executorService,并期望它由多个线程完成。

情景很简单。

我初始化大小为5的线程池。然后我将工作分配给执行者服务。  但我发现只有一个线程正在处理这项工作。

不应该由池中可用的多个线程处理。 代码:

        ExecutorService executorService = Executors.newFixedThreadPool(5);

executorService.execute(new Runnable() {
    public void run() {
        int k =0;
        while (k++<5){
        System.out.println("Active Thread "+Thread.currentThread().getName());
        }
    }
});

这是完整的代码 jdoodle.com/a/bRP

我得到的输出是

Active Thread pool-1-thread-1
Active Thread pool-1-thread-1
Active Thread pool-1-thread-1
Active Thread pool-1-thread-1
Active Thread pool-1-thread-1

我期待的输出是:

参与工作的更多主题。

修改

基本上我想使用ExecutorService复制此行为

package Threading;

public class ThreadDeleteme {
    public static void main(String[] args) {
        Work w = new Work();
        Thread t1 = new Thread(w);
        t1.setName("Thread 1 ");
        Thread t2 = new Thread(w);
        t2.setName("Thread 2 ");
        Thread t3 = new Thread(w);
        t3.setName("Thread 3 ");
        t1.start();
        t2.start();
        t3.start();
        System.out.println("ThreadDeleteme.main()");
    }

}

class Work implements Runnable {
    Integer i = 0;

    @Override
    public void run() {
        while (i < 100) {
            System.out.println(Thread.currentThread().getName() + " " + i);

                i++;
            }
    }
}

你能否告诉我我的理解在哪里出错。

由于

3 个答案:

答案 0 :(得分:3)

您需要创建许多任务,而只创建一个任务。所以,像下面这样的东西应该适合你。但是,如果任务的完成速度比创建另一个任务的速度快,那么在任何情况下它都可能无法击中所有线程。

   ExecutorService executorService = Executors.newFixedThreadPool(5);

    int k =0;
    while (k++<5){
        executorService.execute(new Runnable() {
             public void run() {
                 System.out.println("Active Thread "+Thread.currentThread().getName());
        });
      }
 };

答案 1 :(得分:0)

您创建了一个线程,用于打印当前线程名称(= Active Thread pool-1-thread-1)5次。 制作5个可运行的实例并将它们提交给执行程序服务

[UPDATE]

public class Main {
    public static void main(String[] args) {
        Work w = new Work();
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        executorService.submit(w);
        executorService.submit(new Work()); // set new runnable tasks here
        executorService.submit(new Work());
        System.out.println("ThreadDeleteme.main()");

    }

}

class Work implements Runnable {
    Integer i = 0;

    @Override
    public void run() {
        while (i < 100) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            i++;
        }
    }
}

对于命名问题, 您必须使用ExecutorService实现,该实现允许您设置用于创建线程的ThreadFactory 1,传递创建具有正确名称的线程的实例。 ThreadFactory DOC

答案 2 :(得分:0)

您正在使用相同的线程来打印循环,但是没有使用不同的线程调用方法,线程不会自动划分任务,您必须为它们分配任务。您的main()方法看起来像< / p>

public static void main(String[] args){

     ExecutorService executorService = Executors.newFixedThreadPool(5); //here you defined thread pool size
     executorService.submit(new Work()); //added first thread to pool
     executorService.submit(new Work()); //added second thread
     executorService.submit(new Work()); //added third thread
 }

你的工作班:

class Work implements Runnable {
int i = 0;

@Override
public void run() {
    while (i < 100) {
        System.out.println(Thread.currentThread().getName() + " " + i);

            i++;
        }
}

}