怎么设置task的最大线程数
- //--------------------------------------------------------------------------
- //
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //
- // File: LimitedConcurrencyTaskScheduler.cs
- //
- //--------------------------------------------------------------------------
- using System.Collections.Generic;
- using System.Linq;
- namespace System.Threading.Tasks.Schedulers
- {
- /// <summary>
- /// Provides a task scheduler that ensures a maximum concurrency level while
- /// running on top of the ThreadPool.
- /// </summary>
- public class LimitedConcurrencyLevelTaskScheduler : TaskScheduler
- {
- /// <summary>Whether the current thread is processing work items.</summary>
- [ThreadStatic]
- private static bool _currentThreadIsProcessingItems;
- /// <summary>The list of tasks to be executed.</summary>
- private readonly LinkedList<Task> _tasks = new LinkedList<Task>(); // protected by lock(_tasks)
- /// <summary>The maximum concurrency level allowed by this scheduler.</summary>
- private readonly int _maxDegreeOfParallelism;
- /// <summary>Whether the scheduler is currently processing work items.</summary>
- private int _delegatesQueuedOrRunning = ; // protected by lock(_tasks)
- /// <summary>
- /// Initializes an instance of the LimitedConcurrencyLevelTaskScheduler class with the
- /// specified degree of parallelism.
- /// </summary>
- /// <param name="maxDegreeOfParallelism">The maximum degree of parallelism provided by this scheduler.</param>
- public LimitedConcurrencyLevelTaskScheduler(int maxDegreeOfParallelism)
- {
- if (maxDegreeOfParallelism < ) throw new ArgumentOutOfRangeException("maxDegreeOfParallelism");
- _maxDegreeOfParallelism = maxDegreeOfParallelism;
- }
- /// <summary>Queues a task to the scheduler.</summary>
- /// <param name="task">The task to be queued.</param>
- protected sealed override void QueueTask(Task task)
- {
- // Add the task to the list of tasks to be processed. If there aren't enough
- // delegates currently queued or running to process tasks, schedule another.
- lock (_tasks)
- {
- _tasks.AddLast(task);
- if (_delegatesQueuedOrRunning < _maxDegreeOfParallelism)
- {
- ++_delegatesQueuedOrRunning;
- NotifyThreadPoolOfPendingWork();
- }
- }
- }
- /// <summary>
- /// Informs the ThreadPool that there's work to be executed for this scheduler.
- /// </summary>
- private void NotifyThreadPoolOfPendingWork()
- {
- ThreadPool.UnsafeQueueUserWorkItem(_ =>
- {
- // Note that the current thread is now processing work items.
- // This is necessary to enable inlining of tasks into this thread.
- _currentThreadIsProcessingItems = true;
- try
- {
- // Process all available items in the queue.
- while (true)
- {
- Task item;
- lock (_tasks)
- {
- // When there are no more items to be processed,
- // note that we're done processing, and get out.
- if (_tasks.Count == )
- {
- --_delegatesQueuedOrRunning;
- break;
- }
- // Get the next item from the queue
- item = _tasks.First.Value;
- _tasks.RemoveFirst();
- }
- // Execute the task we pulled out of the queue
- base.TryExecuteTask(item);
- }
- }
- // We're done processing items on the current thread
- finally { _currentThreadIsProcessingItems = false; }
- }, null);
- }
- /// <summary>Attempts to execute the specified task on the current thread.</summary>
- /// <param name="task">The task to be executed.</param>
- /// <param name="taskWasPreviouslyQueued"></param>
- /// <returns>Whether the task could be executed on the current thread.</returns>
- protected sealed override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
- {
- // If this thread isn't already processing a task, we don't support inlining
- if (!_currentThreadIsProcessingItems) return false;
- // If the task was previously queued, remove it from the queue
- if (taskWasPreviouslyQueued) TryDequeue(task);
- // Try to run the task.
- return base.TryExecuteTask(task);
- }
- /// <summary>Attempts to remove a previously scheduled task from the scheduler.</summary>
- /// <param name="task">The task to be removed.</param>
- /// <returns>Whether the task could be found and removed.</returns>
- protected sealed override bool TryDequeue(Task task)
- {
- lock (_tasks) return _tasks.Remove(task);
- }
- /// <summary>Gets the maximum concurrency level supported by this scheduler.</summary>
- public sealed override int MaximumConcurrencyLevel { get { return _maxDegreeOfParallelism; } }
- /// <summary>Gets an enumerable of the tasks currently scheduled on this scheduler.</summary>
- /// <returns>An enumerable of the tasks currently scheduled.</returns>
- protected sealed override IEnumerable<Task> GetScheduledTasks()
- {
- bool lockTaken = false;
- try
- {
- Monitor.TryEnter(_tasks, ref lockTaken);
- if (lockTaken) return _tasks.ToArray();
- else throw new NotSupportedException();
- }
- finally
- {
- if (lockTaken) Monitor.Exit(_tasks);
- }
- }
- }
- }
测试:
输出结果:
参考:
http://msdn.microsoft.com/en-us/library/ee789351(v=vs.110).aspx
https://code.msdn.microsoft.com/Samples-for-Parallel-b4b76364/view/SourceCode#content
怎么设置task的最大线程数的更多相关文章
- 线程池大小设置,CPU的核心数、线程数的关系和区别,同步与堵塞完全是两码事
线程池应该设置多少线程合适,怎么样估算出来.最近接触到一些相关资料,现作如下总结. 最开始接触线程池的时候,没有想到就仅仅是设置一个线程池的大小居然还有这么多的学问,汗颜啊. 首先,需要考虑到线程池所 ...
- JAVA之工作线程数究竟要设置多少
一.需求缘起 Web-Server通常有个配置,最大工作线程数,后端服务一般也有个配置,工作线程池的线程数量,这个线程数的配置不同的业务架构师有不同的经验值,有些业务设置为CPU核数的2倍,有些业务设 ...
- .NET线程池最大线程数的限制-记一次IIS并发瓶颈
.NET ThreadPool 最大线程数的限制 IIS并发瓶颈,有几个地方,IIS线程池的最大队列数,工作进程数,最大并发数.这些这里就不展开.主要是最近因为过度使用Task 导致的线程数占用过多, ...
- IIS并发瓶颈线程数的限制
.NET线程池最大线程数的限制-记一次IIS并发瓶颈 https://www.cnblogs.com/7rhythm/p/9964543.html .NET ThreadPool 最大线程数的限制 I ...
- 通过设置线程池的最小线程数来提高task的效率,SetMinThreads。
http://www.cnblogs.com/Charltsing/p/taskpoolthread.html task默认对线程的调度是逐步增加的,连续多次运行并发线程,会提高占用的线程数,而等若干 ...
- Nginx设置线程数为整机内核数的俩倍!
Nginx设置线程数为整机内核数的俩倍!
- Tomcat设置最佳线程数总结
最佳线程数: 性能压测的情况下,起初随着用户数的增加,QPS会上升,当到了一定的阀值之后,用户数量增加QPS并不会增加,或者增加不明显,同时请求的响应时间却大幅增加.这个阀值我们认为是最佳线程数. 为 ...
- tomcat最大线程数的设置(转)
1.Tomcat的server.xml中连接器设置如下 <Connector port="8080" maxThreads="150" minSpareT ...
- JMeter命令行方式运行时动态设置线程数及其他属性(动态传参)
在使用JMeter进行性能测试时,以下情况经常出现: 1.测试过程中,指定运行的线程数.指定运行循环次数不断改变: 2.访问的目标地址发生改变,端口发生改变,需要改写脚本. 上面的问题在GUI中,直接 ...
随机推荐
- Unity 区分不同平台
问题:公司开发的游戏实在android平台上运行,但是我们是在windows平台下进行开发,OK ,经常有些地方开发完之后就要换到android上面,能区分平台的不同就可以对代码做区分处理 回答:un ...
- SET GLOBAL slow_query_log=1
- Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL SHOW VARIABLES LI ...
- Cluster analysis
https://en.wikipedia.org/wiki/Cluster_analysis Cluster analysis or clustering is the task of groupin ...
- prior knowledge
https://en.wikipedia.org/wiki/Bayes'_theorem For example, if cancer is related to age, then, using B ...
- ifarm 子 父页面方法如何互调
1.iframe子页面调用父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: 代码如下: window.parent.a(); 子页面取父页 ...
- taocode
http://code.taobao.org/project/lang/list/Go/1/
- sql CRUD 增删改查复习汇总
1.创建数据库create database 数据库名称删除数据库drop database 数据库名称2.创建表create table 表名( 列名 类型(长度) 自增长 主键 非空,)自增 ...
- DevExpress的所有功能介绍
https://www.devexpress.com/Subscriptions/New-2016-2.xml?utm_source=AnnounceTry&utm_medium=WhatsN ...
- linux epoll 简单demo
一个简单的epoll demo ,同时接受多个客户端连接,并把接收到的字符串转化为大写字母返回给客户端 #include<stdio.h> #include<arpa/inet.h& ...
- java分形树
import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; /** * * @ ...