C# 多线程、控制线程数提高循环输出效率
C#多线程及控制线程数量,对for循环输出效率。
虽然输出不规律,但是效率明显提高。
思路:
如果要删除1000条数据,只使用for循环,则一个接着一个输出。所以,把1000条数据分成seed段,每段10条数据。
int seed = Convert.ToInt32(createCount.Value) % 10 == 0 ? Convert.ToInt32(createCount.Value) / 10 : Convert.ToInt32(createCount.Value) / 10 + 1;
注:createCount.Value的值是具体输出数据的数量
这里把数据分配给seed个线程去处理,每个线程只输出10个数据。
int threadCountTmp = 0;//任务线程分派数 private void btnCreate_Click(object sender, EventArgs e) { int seed = Convert.ToInt32(createCount.Value) % 10 == 0 ? Convert.ToInt32(createCount.Value) / 10 : Convert.ToInt32(createCount.Value) / 10 + 1; for (int i = 0; i < seed; i++) { Thread threadTmp = new Thread(new ParameterizedThreadStart(TempOut)); threadTmp.Start(i); threadCountTmp++; Application.DoEvents();//响应窗口状态 while (true) { if (threadCountTmp < 10) break; }//推拉窗式控制多线程 线程数10 } } //分段后的数据发布给其它线程 public void TempOut(object o) { int tmp=Convert.ToInt32(o)*10; int i = tmp; for (; i < (tmp+10<=createCount.Value?tmp+10:createCount.Value); i++) { Thread thread = new Thread(new ParameterizedThreadStart(ResultOut)); thread.Start(i); threadCount++; while (true) { if (threadCount < 10) break; }//推拉窗式控制多线程 线程数10 } threadCountTmp--; }
分段后,再将分段后的数据分配给其它线程来处理,这样就能多线程同时工作了,由于要对控件操作,所以使用多线程的话要依靠委托来实现多线程对控件的控制。所以最后一步的输出,如下:
delegate void TextTmp(object o);//声明委托 int threadCount = 0;//任务线程 //委托函数 public void ResultOut(object o) { if (!txtResult.InvokeRequired) { txtResult.Text = "\n" + f_groundcode.Text + "," + f_ticketno.Text + DateTime.Now.ToLongDateString().Replace("-", "") + GetZero(6 - o.ToString().Length) + o.ToString() + "," + DateTime.Now.ToLongDateString().Replace("-", "") + DateTime.Now.ToLongTimeString().Replace(":", "") + txtResult.Text; } else { TextTmp tmpDel = new TextTmp(ResultOut); this.Invoke(tmpDel,o); } threadCount--; }
因为我的数据要保证位数,所以要对0做简单处理。例如 我要输出
000000
000001
000002
000003
........
从上面的代码可以看出,我是使用for来递增的。所以是整型,前面的0随着数值的大小不断改变个数。
//处理数字前面有多少个0 private string GetZero(int leng) { string result = ""; for (int i = 0; i < leng; i++) { result += "0"; } return result; }
好了。简单的多线程处理。希望大家可以学习。欢迎大家指导~~
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
SpringBoot(四)_Spring Data JPA的使用
JPA 绝对是简化数据库操作的一大利器。 概念 首先了解 JPA 是什么? JPA(Java Persistence API)是 Sun 官方提出的 Java 持久化规范。它为 Java 开发人员提供了一种对象/关联映射工具来管理 Java 应用中的关系数据。 注意:JPA 是一套规范,不是一套产品,那么像 Hibernate、TopLink、JDO 它们是一套产品,如果说这些产品实现了这个 JPA 规范,那么就可以叫它们为 JPA 的实现产品。 实例操作 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</arti...
- 下一篇
监控目前所有连接SQL SERVER的用户信息
原文: 监控目前所有连接SQL SERVER的用户信息 if object_id('p_getlinkinfo','P')is not null drop proc p_getlinkinfo go create proc p_getlinkinfo @dbname sysname=null, --要查詢的數據庫名,默認表示所有 @includeip bit=0 --是否顯示IP信息 as begin declare @dbid int set @dbid=db_id(@dbname) if object_id('tempdb..#tb')is not null drop table #tb if object_id('tempdb..#ip')is not null drop table #ip create table #tb (id int identity(1,1), dbname sysname, hostname nchar(128), loginname nchar(128), net_address nchar(12), net_ip nvarchar(15), pro...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Windows10,CentOS7,CentOS8安装Nodejs环境
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS关闭SELinux安全模块
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7设置SWAP分区,小内存服务器的救世主