在Java ThreadPool上感到困惑 [英] Confused on Java ThreadPool

查看:84
本文介绍了在Java ThreadPool上感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我遇到这个
链接 http://www.javacodegeeks.com/2013/01/java-thread-pool-example-using-executors-and-threadpoolexecutor .html ,我对此更加困惑,这里是来自页面的代码,

It is my first time to use Java Thread Pool for my new project, after I came across this link http://www.javacodegeeks.com/2013/01/java-thread-pool-example-using-executors-and-threadpoolexecutor.html, I am more confused on this, here is the code from the page,

package com.journaldev.threadpool;

public class WorkerThread implements Runnable {

    private String command;

    public WorkerThread(String s){
        this.command=s;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+' Start. Command = '+command);
        processCommand();
        System.out.println(Thread.currentThread().getName()+' End.');
    }

    private void processCommand() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString(){
        return this.command;
    }
}


package com.journaldev.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SimpleThreadPool {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread('' + i);
            executor.execute(worker);
          }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println('Finished all threads');
    }

}

代码中

,固定大小创建了池并创建了10个工作线程,我是对的吗?

in the code, a fixed size pool is created and 10 worker threads are created, am I right?

线程池应该可以减轻系统的负担,相反,在上面的代码中,我认为除了工作线程之外还通过创建池来增加负担。为什么还要使用线程池呢?

The thread pool is supposed to decrease the burden of a system, on the contrary, in the above code, I think it increases the burden by creating the pool in addition to the worker threads. why bother to use the thread pool?

谁能解释一下?
谢谢

Can anyone explain? Thanks

我还在StackOverflow上阅读了这篇文章
http://stackoverflow.com/questions/19765904/how-threadpool-re-use-threads-and-how-它工作
它也没有帮助我。

I also read this post on StackOverflow http://stackoverflow.com/questions/19765904/how-threadpool-re-use-threads-and-how-it-works it did not help me either.

推荐答案

这令人困惑,因为Runnables是名为WorkerThread,但它们不扩展java.lang.Thread,它们只是实现Runnable的对象。实现Runnable允许您指定需要执行的任务,而无需实例化实际的Thread对象。在您的示例中创建的唯一线程是主线程和Executor创建的线程。

This is confusing because the Runnables are named WorkerThread, but they don't extend java.lang.Thread, they're just objects that implement Runnable. Implementing Runnable lets you specify a task that needs to be executed without having to instantiate an actual Thread object. The only threads created in your example are the main thread and the ones created by the Executor.

请注意,即使您更改此代码以使WorkerThread扩展Thread,只要代码没有调用它们就会启动它就不会导致更多的线程实际运行。构造一个线程对象涉及一些事情,比如检查安全管理器和初始化threadlocals,但它实际上并没有在操作系统级别做任何事情来分配线程。就Executor而言,它们只是Runnables,它将使用线程池的线程执行它们。

Note that, even if you change this code to make WorkerThread extend Thread, as long as the code doesn't call start on them it wouldn't result in more threads actually running. Constructing a thread object involves some things like checking with the Security Manager and initializing threadlocals, but it doesn't actually do anything at the OS-level to allocate a thread. As far as the Executor is concerned they're just Runnables, it would execute them using the threadpool's threads.

这篇关于在Java ThreadPool上感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆