Java 杂谈

Java中的线程池和作用,有必要了解一下

2018-08-02  本文已影响45人  maoqitian

在Java开发中,多线程执行任务是很常见的,Java也提供了线程类Thread来让我们方便创建一个线程如下代码所示

           new Thread(){
                @Override
                public void run() {
                    .....

                }
            }.start();

Java SE5d的java.util.concurrent包 提供了Executor(执行器)来管理线程对象。Executor是一个接口,而ExecutorService继承了Excutor接口,ExecutorService是一个具有生命周期的Executor,它知道如何构建恰当的上下文来执行Runnable对象。而ExecutorService对象是使用Executors的静态方法得到Java中的线程池

//Executor接口实现

public interface Executor {

    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the {@code Executor} implementation.
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);
}

好了,这就是我所了解的线程池知识,如果有错,请给我留言指出,大家一起学习进步。

上一篇 下一篇

猜你喜欢

热点阅读