当前位置: 首页 > news >正文

如何把自己的网站推广百度关键词查询排名

如何把自己的网站推广,百度关键词查询排名,暴雪网易最新消息,做qq游戏的视频秀网站一、线程池的作用 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认堆栈大小,以默认的优先级运行,并处于多线程单元中。如果某个线程在托管…

一、线程池的作用

        线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认堆栈大小,以默认的优先级运行,并处于多线程单元中。如果某个线程在托管代码中空闲(如正在等待某个事件),则线程池将插入另一个辅助线程来使所有处理器保持繁忙。如果所有线程池线程都始终保持繁忙,但队列中包含挂起的工作,则线程池将在一段时间之后创建另一个辅助线程。但线程的数目永远不会超过最大值。超过最大值的其他线程可以排队,但它们要等到其他线程完成后才启动。

线程池的使用范围: 

(1)不需要前台执行的线程。

(2)不需要在使用线程具有特定的优先级。

(3)线程的执行时间不易过长,否则会使线程阻塞。由于线程池具有最大线程数限制,因此大量阻塞的线程池线程可能会阻止任务启动。

(4)不需要将线程放入单线程单元。所有 ThreadPool 线程均不处于多线程单元中。

(5)不需要具有与线程关联的稳定标识,或使某一线程专用于某一任务。
 

二、常用方法介绍

1.ThreadPool.QueueUserWorkItem

        该方法是线程池中最主要的方法,ThreadPool.QueueUserWorkItem 方法是用于将工作项提交到线程池队列中的方法。当你需要执行一个方法但不想创建一个新的线程时,可以使用该方法。这个方法会将工作项放到一个线程池队列中,并由线程池中的一个线程来执行该工作项。

 ThreadPool.QueueUserWorkItem(WaitCallback(DoWork), object)

该方法主要是两个参数,第一个是WaitCallback,第二个是一个object,object参数可以作为WaitCallback方法的参数传入。

public delegate void WaitCallback(object state);

 WaitCallback是一个委托类型,委托参数类型是object定义,因此传入WaitCallback的方法也应当是object类型。

codepublic static void DoWork(object state)
{// 执行一些操作,使用传递进来的状态对象
}static void Main(string[] args)
{// 将 DoWork 方法添加到线程池中ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), someStateObject);
}

someStateObject是DoWork的参数进行传入,然后开启线程。

2.SetMinThreads和SetMaxThreads

SetMinThreads和SetMaxThreads是线程池中最小线程数和最大线程数 


// 参数:// workerThreads:// 要由线程池根据需要创建的新的最小工作程序线程数。// completionPortThreads:// 要由线程池根据需要创建的新的最小空闲异步 I/O 线程数。// 返回结果:如果更改成功,则为 true;否则为 false。[SecuritySafeCritical]public static bool SetMinThreads(int workerThreads, int completionPortThreads);// 参数:// workerThreads:// 线程池中辅助线程的最大数目。// completionPortThreads:// 线程池中异步 I/O 线程的最大数目。// 返回结果:如果更改成功,则为 true;否则为 false。[SecuritySafeCritical]public static bool SetMaxThreads(int workerThreads, int completionPortThreads)例如:ThreadPool.SetMinThreads(1,1);ThreadPool.SetMaxThreads(5, 5);

3.ManualResetEvent用法

1.ManualResetEvent 调用一次Set()后将允许恢复所有被阻塞线程。需手动在调用WaitOne()之后调用Reset()重置信号量状态为非终止,然后再次调用WaitOne()的时候才能继续阻塞线程,反之则不阻塞

2.AutoResetEvent,调用一次Set()只能继续被阻塞的一个线程,多次调用Set()才行,但不需手动调用Reset();再次调用WaitOne()的时候又能阻塞线程,也是和前者的区别

3.两者单个实例均可阻塞一个或多个线程,在多个线程中调用 主线程 创建的 两者单个实例.WaitOne(),前提是两者实例必须是非终止状态

4.两者实例化构造参数解释

public AutoResetEvent(bool initialState);

true:设置终止状态。相当于调用了Set(),即首次不会被WaitOne()阻塞,下次执行WaitOne()才会被阻塞

false:设置非终止状态。遇到WaitOne()立即阻塞所在的一个或多个线程

具体参考一下文章:

C#学习(二十八)——ManualResetEvent的理解和使用-CSDN博客

三、ThreadPool代码

代码1:关于ManualResetEvent用法

using System;
using System.Threading;public class Example
{// mre is used to block and release threads manually. It is// created in the unsignaled state.private static ManualResetEvent mre = new ManualResetEvent(false);static void Main(){Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");//中文注释1:开启三个线程,每个线程开启后调用WaitOne()阻塞。for(int i = 0; i <= 2; i++){Thread t = new Thread(ThreadProc);t.Name = "Thread_" + i;t.Start();}Thread.Sleep(500);Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +"\nto release all the threads.\n");Console.ReadLine();//中文注释2:只有当Set()后才会执行WaitOne()后的代码mre.Set();Thread.Sleep(500);Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +"\ndo not block. Press Enter to show this.\n");Console.ReadLine();//中文注释3:继续再开两个线程,仍然调用WaitOne(),但是不会阻塞,会继续执行for(int i = 3; i <= 4; i++){Thread t = new Thread(ThreadProc);t.Name = "Thread_" + i;t.Start();}Thread.Sleep(500);Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +"\nwhen they call WaitOne().\n");Console.ReadLine();//中文注释4:只有Reset()后,下面再开线程就会继续被阻塞mre.Reset();// Start a thread that waits on the ManualResetEvent.Thread t5 = new Thread(ThreadProc);t5.Name = "Thread_5";t5.Start();Thread.Sleep(500);Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");Console.ReadLine();//中文注释5:再次Set(),就可以了mre.Set();// If you run this example in Visual Studio, uncomment the following line://Console.ReadLine();}private static void ThreadProc(){string name = Thread.CurrentThread.Name;Console.WriteLine(name + " starts and calls mre.WaitOne()");mre.WaitOne();Console.WriteLine(name + " ends.");}
}/* This example produces output similar to the following:Start 3 named threads that block on a ManualResetEvent:Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()When all three threads have started, press Enter to call Set()
to release all the threads.Thread_2 ends.
Thread_0 ends.
Thread_1 ends.When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.Press Enter to call Reset(), so that threads once again block
when they call WaitOne().Thread_5 starts and calls mre.WaitOne()Press Enter to call Set() and conclude the demo.Thread_5 ends.

代码2:


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace ConsoleApplication1
{class Program{static void Main(string[] args){const int times = 10;  //开线程数ManualResetEvent[] mre = new ManualResetEvent[times];   //1、定义开线程数Random random = new Random();   //随机数Console.WriteLine("开始 {0} 任务", times);for (int i = 0; i < times; i++)   //2、循环这10个线程{mre[i] = new ManualResetEvent(false);  //3、初始化每个线程:设置false表示无信号,将使WaitOne阻塞也就是线程等待count c = new count(random.Next(1, 1000), mre[i]);   //借助类传参ThreadPool.QueueUserWorkItem(c.ThreadPoolCallback, i);   //4、为每个线程安排任务}WaitHandle.WaitAll(mre);    //6、让主线程等待所有线程完成(池中线程数不能多于64个)Console.WriteLine("所有线程完成!");Console.Read();}}class count{private int ramNum;   //存放随机数private ManualResetEvent threadSta;   //线程状态private int total;    //存放线程计算结果/// <summary>/// 传递数据/// </summary>/// <param name="ramnum">保存随机数</param>/// <param name="mre">线程状态</param>public count(int ramnum, ManualResetEvent mre){ramNum = ramnum;threadSta = mre;}/// <summary>/// 线程/// </summary>/// <param name="threadParam"></param>public void ThreadPoolCallback(Object threadParam){int threadIndex = (int)threadParam;Console.WriteLine("线程 {0} 启动", threadIndex);total = docount(ramNum);Console.WriteLine("线程执行结果: {0}", total);threadSta.Set();  //5、设置每个线程为有信号状态:通知WaitOne不再阻塞}/// <summary>/// 从0开始加到传过来数/// </summary>/// <param name="ramNum">传过来的数:产生的随机数</param>/// <returns>返回相加的结果</returns>public int docount(int ramNum){int sum = 0;for (int i = 0; i <= ramNum; i++){sum += i;}return sum;}}
}

参考文献:

C#中的线程池使用方法_c# 线程池-CSDN博客

C#多线程--线程池(ThreadPool)_c# 主线程和 线程池-CSDN博客

C#知识点讲解之ManualResetEvent类的使用-CSDN博客

C#线程池实例(多参)_c# 线程池多参数-CSDN博客

C#多线程和线程池_c#线程池和线程的区别-CSDN博客

http://www.shuangfujiaoyu.com/news/24636.html

相关文章:

  • 淘宝客怎么做网站推广国内做seo最好的公司
  • 网站应该怎么做运维谷歌网页版
  • 杭州微跑网站建设公司南京seo全网营销
  • 设计师的工作内容优优群排名优化软件
  • 好的响应式网站有哪些网站排名优化师
  • django 做网站 原理百度搜索关键词统计
  • 怎么才能访问自己做的网站卢松松外链工具
  • 邢台做网站优化哪儿好seo站长工具
  • 网站建设服务公怎样做网站推广啊
  • 群晖wordpress如何连接mysqlseo优化代理
  • 网上营销的好处seo厂商
  • 做推广适合哪些网站吗直播:英格兰vs法国
  • 林管局网站建设方案企点qq
  • 中英文网站怎么实现太原百度搜索排名优化
  • 一级做a爱免费网站怎么投放网络广告
  • 河北农业建设信息网站软文300字介绍商品
  • 优秀网站例子什么建站程序最利于seo
  • 百度做玻璃钢的网站良品铺子网络营销策划书
  • 网站备案用户名忘了怎么办域名访问网站入口
  • 推广你公司网站windows优化大师
  • 网站必须做API接口吗国外免费建站网站搭建
  • 南宁市做网站的公司百度推广竞价技巧
  • 苹果cms网站广告怎么做wp博客seo插件
  • 政府网站建设指导意见西安seo关键词推广
  • 做kegg网站直播引流推广方法
  • 网站建设空间申请长春网站关键词推广
  • 那里做网站比较好排名优化价格
  • 四川监狱网站建设深圳搜索引擎优化收费
  • 公司网站怎么更新需要怎么做百度一下官网手机版
  • 企业建设营销网站的基本步骤2024年重大政治时事汇总