所以我有一个程序产生线程(~5-150)执行一堆任务.最初我使用的是FixedThreadPool
因为这个类似的问题表明它们更适合长寿命的任务,而且由于我对多线程的知识非常有限,我认为线程的平均寿命(几分钟)" 长寿 ".
但是,我最近添加了生成其他线程的功能,这样做超过了我设置的线程限制.在这种情况下,最好猜测并增加我可以允许的线程数或切换到一个CachedThreadPool
所以我没有浪费线程?
初步尝试它们,似乎没有什么区别,所以我倾向于选择CachedThreadPool
公正以避免浪费.但是,线程的生命周期是否意味着我应该选择一个FixedThreadPool
并且只处理未使用的线程?这个问题使得看起来这些额外的线程没有浪费,但我希望澄清.
java multithreading executorservice java.util.concurrent threadpool
Scala的期货线程池有多大?
我的Scala应用程序创建了数百万future {}
s,我想知道我是否可以通过配置线程池来优化它们.
谢谢.
在我的一个有点聚合的项目中,我从网上解析了feed,podcast等.
如果我使用顺序方法,考虑到大量资源,处理所有资源需要相当长的时间(因为网络问题和类似的东西);
foreach(feed in feeds)
{
read_from_web(feed)
parse(feed)
}
Run Code Online (Sandbox Code Playgroud)
所以我想实现并发,并且无法决定我是否应该使用ThreadPools来处理工作线程,或者只是依靠TPL来对它进行排序.
ThreadPools肯定会用工作线程处理我的工作,我会得到我所期望的(在多核CPU环境中,其他核心也将被利用).
但我仍然想考虑TPL,因为它推荐的方法,但我有点担心它.首先,我知道TPL使用ThreadPools,但增加了额外的决策层.我主要关心的是单核环境存在的情况.如果我没错,TPL从一开始就用一个数量的工作线程开始,这些工作线程数等于可用的CPU核心数.我担心TPL会产生与我的IO绑定案例的顺序方法类似的结果.
因此,对于IO绑定操作(在我的情况下从Web读取资源),最好是使用ThreadPools来控制事物,还是更好地依赖于TPL?TPL也可以用于IO绑定场景吗?
更新:我主要担心的是 - 在单核CPU环境中,TPL只是表现得像顺序方式,还是会提供并发性?我已经阅读了使用Microsoft .NET的并行编程,所以这本书却找不到确切的答案.
注意:这是我之前的问题的重新措辞[ 是否可以将线程并发和并行一起使用?这是错误的措辞.
c# multithreading parallel-extensions threadpool task-parallel-library
下面有什么区别
ThreadPool.QueueUserWorkItem
Run Code Online (Sandbox Code Playgroud)
VS
Task.Factory.StartNew
Run Code Online (Sandbox Code Playgroud)
如果对于某些长时间运行的任务调用上述代码500次,是否意味着将占用所有线程池线程?
或者TPL(第二选项)是否足够聪明,只能占用少于或等于处理器数量的线程?
我正在尝试编写一个解决方案,其中单个线程生成可以并行执行的I/O密集型任务.每个任务都有重要的内存数据.所以我希望能够限制暂时待处理的任务数量.
如果我像这样创建ThreadPoolExecutor:
ThreadPoolExecutor executor = new ThreadPoolExecutor(numWorkerThreads, numWorkerThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(maxQueue));
Run Code Online (Sandbox Code Playgroud)
然后在队列填满并且所有线程都已忙时executor.submit(callable)
抛出RejectedExecutionException
.
executor.submit(callable)
当队列已满且所有线程都忙时,我该怎么做才能阻塞?
编辑:我试过这个:
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
Run Code Online (Sandbox Code Playgroud)
它有点实现了我想要实现的效果但是以一种不雅的方式(基本上被拒绝的线程在调用线程中运行,因此这阻止了调用线程提交更多).
编辑:(提问后5年)
对于阅读此问题及其答案的任何人,请不要将接受的答案作为一个正确的解决方案.请仔细阅读所有答案和评论.
我在Java 1.6中使用ExecutoreService,简单地开始
ExecutorService pool = Executors.newFixedThreadPool(THREADS).
Run Code Online (Sandbox Code Playgroud)
当我的主线程完成时(以及线程池处理的所有任务),此池将阻止我的程序关闭,直到我显式调用
pool.shutdown();
Run Code Online (Sandbox Code Playgroud)
我可以避免不得不通过某种方式将此池使用的内部线程管理变为deamon线程来调用它吗?或者我在这里遗漏了一些东西.
寻找一些简单的线程池实现的示例代码(C#).
我在codeproject上找到了一个,但代码库很大,我不需要所有的功能.
无论如何,这更多是出于教育目的.
正如一些人在.NET 4.0中看到的那样,他们已经添加了一个新的命名空间System.Threading.Tasks
,它基本上就是手段,一个任务.使用ThreadPool我只使用了几天.
哪一个更有效,资源消耗更少?(或者总体上更好?)
请告诉我之间的差异ThreadPool
,并Pool
在multiprocessing
模块.当我尝试我的代码时,这是我看到的主要区别:
from multiprocessing import Pool
import os, time
print("hi outside of main()")
def hello(x):
print("inside hello()")
print("Proccess id: ", os.getpid())
time.sleep(3)
return x*x
if __name__ == "__main__":
p = Pool(5)
pool_output = p.map(hello, range(3))
print(pool_output)
Run Code Online (Sandbox Code Playgroud)
我看到以下输出:
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
hi outside of main()
inside hello()
Proccess id: 13268
inside hello()
Proccess id: 11104
inside hello()
Proccess id: …
Run Code Online (Sandbox Code Playgroud) python multiprocessing threadpool python-3.x python-multiprocessing
我正在学习用来ExectorService
汇集threads
和发送任务.我有一个简单的程序如下
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class Processor implements Runnable {
private int id;
public Processor(int id) {
this.id = id;
}
public void run() {
System.out.println("Starting: " + id);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("sorry, being interupted, good bye!");
System.out.println("Interrupted "+Thread.currentThread().getName());
e.printStackTrace();
}
System.out.println("Completed: " + id);
}
}
public class ExecutorExample {
public static void main(String[] args) {
Boolean isCompleted=false;
ExecutorService executor = Executors.newFixedThreadPool(2);
for(int i=0; i<5; i++) {
executor.execute(new …
Run Code Online (Sandbox Code Playgroud) threadpool ×10
c# ×4
java ×4
.net ×1
c#-4.0 ×1
concurrency ×1
future ×1
java-threads ×1
python ×1
python-3.x ×1
scala ×1
task ×1