简体   繁体   English

Java线程

[英]Java Threads

How do I implement a ThreadPoolExecutor? 如何实现ThreadPoolExecutor? What needs to be done to use it? 使用它需要做什么?

The API Specification for the ThreadPoolExecutor class has four constructors which can be used to create an instance of the ThreadPoolExecutor . ThreadPoolExecutor类的API规范具有四个构造函数,可用于创建ThreadPoolExecutor的实例。

The Executors class also has methods such as newCachedThreadPool and newFixedThreadPool methods, but those are only listed as returning an ExecutorService , so it doesn't necessarily have to be a ThreadPoolExecutor . Executors类还具有诸如newCachedThreadPoolnewFixedThreadPool方法之类的方法,但是这些方法仅被列为返回ExecutorService ,因此它不一定是ThreadPoolExecutor

For more general information on Executors and the Java concurrency package, the Lesson: Concurrency from The Java Tutorials has more information. 有关Executors和Java并Executors更多常规信息, 《 Java教程》中的“ 课程:并发 提供了更多信息。 In particular, the Executors and Thread Pools section may be of interest. 特别是,“ 执行程序线程池”部分可能会很有用。

Executor is a interface. 执行程序是一个接口。 Implementing it allows clients to pass you runnable tasks, which are then executed in a particular manner. 实现它可以使客户端传递可运行的任务,然后以特定的方式执行这些任务。 ThreadPoolExecutor is one class implementing that interface (and ExecutorService). ThreadPoolExecutor是实现该接口(和ExecutorService)的一类。 It uses a thread pool so you can have multiple threads executing jobs without needing a new thread for every job. 它使用一个线程池,因此您可以有多个线程来执行作业,而无需为每个作业使用新的线程。 ThreadPoolExecutor can be subclassed, or you can just instantiate it with one of the public constructors. ThreadPoolExecutor可以被子类化,或者您可以使用公共构造函数之一实例化它。

Other methods, such as Executors.newFixedThreadPool , return a thread pool of some kind though not (necessarily) a ThreadPoolExecutor. 其他方法,例如Executors.newFixedThreadPool ,尽管(不是必需)不是ThreadPoolExecutor,但返回某种类型的线程池。

Java Concurrency in Practice - Addison Wesley ISBN:0321349601 I used this book to learn about Executors. Java并发实践-Addison Wesley ISBN:0321349601我用这本书来学习执行器。 I has a bunch of good examples you can steal. 我有很多可以窃取的好例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM