英文:
what happens if we pass threads in executorService execute call in Java multithreading
问题
请看这两个示例:
示例 1
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
//工作内容
for (int i = 0 ; i < 5 ; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
executorService.execute(thread1);
executorService.shutdown();
}
示例 2
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new Runnable() {
@Override
public void run() {
//工作内容
for (int i = 0 ; i < 5 ; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
executorService.shutdown();
}
两种情况下的结果都是相同的,但是来自StackOverflow的朋友 alexei-kaigorodov 在评论中提到:
"executorService 是线程的替代方案。在 executorService 中没有必要放置线程。首先,创建一个 Runnable,然后将其作为参数放入一个线程并启动该线程,或者将该 runnable 提交给 executorService。"
我希望我对问题的描述很清楚,所以请告诉我将 Runnable 传递给 ExecutorService 与将线程传递给 ExecutorService 之间的区别。
英文:
See these two examples:
EXAMPLE 1
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
//work
for (int i = 0 ; i < 5 ; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
executorService.execute(thread1);
executeService.shutdown();
}
EXAMPLE 2
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new Runnable() {
@Override
public void run() {
//work
for (int i = 0 ; i < 5 ; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
executeService.shutdown();
}
Results are the same in both cases but a friend of mine from StackOverflow alexei-kaigorodov (in this question) said in a comment that
"executorService is an alternative to treads. There is no sense to put threads in executorService. First, create Runnable, and then either put it as a parameter to a thread and start that thread or submit that runnable to an executorService."
I hope I am clear with my question, so please tell me the difference in passing runnable to ExecuteService vs. passing thread to ExecutableService.
答案1
得分: 2
Thread
实现了 Runnable
接口,因此执行器服务将把它视为一个简单的 Runnable
,从而调用 Thread.run()
方法。
这意味着 Thread
本身永远不会被启动,除非你自己调用它的 start()
方法,在这种情况下,结果是不确定的。
因此,你可以说这些代码片段的行为是相同的,你只是将一个 Runnable
传递给了 ExecutorService
,然后通过调用 Runnable.run()
方法来执行它。
英文:
Thread implements Runnable
so the excutor service will accept it as a simple Runnable
, and thus calls the Thread.run()
method.
Which means that the Thread
itself will never be started, unless you call its start()
method yourself, in which case the result is definetely undefined.
So you could say the snippets behave the same, you're only passing a Runnable
to the ExecutorService
and that then executes it by calling the Runnable.run()
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论