ExecutorService 接口执行方法使用 Executors class in java
ExecutorService interface execute method using Executors class in java
请不要关闭这个问题。与问题 What does it mean to "program to an interface"?
无关
我正在尝试学习 ExecutorService 接口及其方法。我使用 Executors class 静态方法 newFixedThreadPool() 创建了一个 ExecutorService 类型的对象,并尝试调用 ExecutorService 接口的 execute() 方法。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class WorkerThread implements Runnable {
private String message;
public WorkerThread(String s) {
this.message = s;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " (Start) message = " + message);
System.out.println(Thread.currentThread().getName() + " (End)");// prints thread name
}
}
public class TestThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);// creating a pool of 5 threads
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);// calling execute method of ExecutorService
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
在上面的代码中,我使用 Executors class newFixedThreadPool 方法创建了一个 ExecutorService 类型的对象,该方法创建了一个包含 5 个线程的池。
但是 executor 是如何在这里调用 execute() 方法的,因为它是 ExecutorService 接口的抽象方法,而 Executors class 也没有任何 execute() 方法?
But How executor is calling execute() method here, as it is an abstract method of ExecutorService interface and the Executors class also don't have any execute() method?
Executors.newFixedThreadPool
returns 实现 ExecutorService
的 class 实例。此实例具有 execute
方法。
见Executors.newFixedThreadPool(int)
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
ThreadPoolExecutor
实现了 execute
方法。请参阅 ThreadPoolExecutor.java#L1327
. The ThreadPoolExecutor
extends AbstractExecutorService
和 AbstractExecutorService
实现 Executor
和 ExecutorService
。
编辑
Hi, could you also share the link of Executors class all method?
Executors中确实没有Execute方法class。它是 ExecutorService class 中的一个方法。您正在调用此方法。实际上它是 Executor 接口的一个方法,ExecutorService class 实现了:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html#execute(java.lang.Runnable)
查看您粘贴的文档中的“所有超级接口”部分:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
请不要关闭这个问题。与问题 What does it mean to "program to an interface"?
无关我正在尝试学习 ExecutorService 接口及其方法。我使用 Executors class 静态方法 newFixedThreadPool() 创建了一个 ExecutorService 类型的对象,并尝试调用 ExecutorService 接口的 execute() 方法。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class WorkerThread implements Runnable {
private String message;
public WorkerThread(String s) {
this.message = s;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " (Start) message = " + message);
System.out.println(Thread.currentThread().getName() + " (End)");// prints thread name
}
}
public class TestThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);// creating a pool of 5 threads
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);// calling execute method of ExecutorService
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
在上面的代码中,我使用 Executors class newFixedThreadPool 方法创建了一个 ExecutorService 类型的对象,该方法创建了一个包含 5 个线程的池。
但是 executor 是如何在这里调用 execute() 方法的,因为它是 ExecutorService 接口的抽象方法,而 Executors class 也没有任何 execute() 方法?
But How executor is calling execute() method here, as it is an abstract method of ExecutorService interface and the Executors class also don't have any execute() method?
Executors.newFixedThreadPool
returns 实现 ExecutorService
的 class 实例。此实例具有 execute
方法。
见Executors.newFixedThreadPool(int)
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
ThreadPoolExecutor
实现了 execute
方法。请参阅 ThreadPoolExecutor.java#L1327
. The ThreadPoolExecutor
extends AbstractExecutorService
和 AbstractExecutorService
实现 Executor
和 ExecutorService
。
编辑
Hi, could you also share the link of Executors class all method?
Executors中确实没有Execute方法class。它是 ExecutorService class 中的一个方法。您正在调用此方法。实际上它是 Executor 接口的一个方法,ExecutorService class 实现了:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html#execute(java.lang.Runnable)
查看您粘贴的文档中的“所有超级接口”部分:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html