MultiThreading与ThreadPoolExecutor [英] MultiThreading Vs ThreadPoolExecutor

查看:151
本文介绍了MultiThreading与ThreadPoolExecutor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我编写的许多应用程序中使用过多线程。在阅读更多内容时,我遇到了 ThreadPoolExecutors 。我无法区分这两种情况。

I have used multithreading in many of applications I wrote . While reading more I came across ThreadPoolExecutors. I couldn't not differentiate between the two scenario wise .

我理解的是,当我有一项任务时,我应该使用多线程我想将任务分成多个小任务来利用CPU并更快地完成工作。当我设置任务并且每个任务可以相互独立运行时,使用 ThreadPoolExecutor

Still what I understand is I should use multithreading when I have a task I want to divide a task in to multiple small tasks to utilize CPU and do the work faster . And use ThreadPoolExecutor when I have a set to tasks and each task can be run independent of each other.

请如果我错了,请纠正我。谢谢

Please correct me if I am wrong . Thanks

推荐答案

ThreadPoolExecutor 只是一个高级API,可以让你在不必处理低级Thread API的情况下在多个线程中运行任务。因此区分多线程和ThreadPoolExecutor并没有多大意义。

A ThreadPoolExecutor is just a high level API that enables you to run tasks in multiple threads while not having to deal with the low level Thread API. So it does not really make sense to differentiate between multithreading and ThreadPoolExecutor.

有许多种类的 ThreadPoolExecutor s,但是大多数都允许多个线程并行运行。通常,您将使用执行服务并使用执行者工厂。

There are many flavours of ThreadPoolExecutors, but most of them allow more than one thread to run in parallel. Typically, you would use an Executor Service and use the Executors factory.

例如, ExecutorService executor = Executors。 newFixedThreadPool(10); 将运行您在10个主题中提交的任务。

For example, a ExecutorService executor = Executors.newFixedThreadPool(10); will run the tasks you submit in 10 threads.

这篇关于MultiThreading与ThreadPoolExecutor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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