博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程的开启与管理
阅读量:4314 次
发布时间:2019-06-06

本文共 2649 字,大约阅读时间需要 8 分钟。

先写一个用于线程的类
work代码
 1  public   class  work
 2      {
 3           // 定义一个线程
 4          Thread thread  =   null ;
 5           // 关键字
 6           string  Key  =   string .Empty;
 7           // 控制while语句的
 8           bool  s  =   false ;
 9           string  value  =   string .Empty;
10           int  i  =   0 ;
11           // 构造方法
12           public  work( string  name)
13          {
14               this .Key  =  name;
15          }
16           // 开启线程,实例化线程
17           public   void  start()
18          {
19              thread  =   new  Thread(read);
20              thread.Name  =  Key;
21              thread.Start();
22          }
23           // 线程开启要执行的方法
24           public   void  read()
25          {
26               while  ( ! s)
27              {                
28                  value  =  thread.Name  +   " 线程第 "   +  i  +   " 次执行 " ;
29                  Thread.Sleep( 2000 );
30                  i ++ ;
31                   //  在这里把你想要处理的方法放进来
32              }
33          }
34           // 停止线程
35           public   void  stop()   // 线程停止
36          {
37              s  =   true ;
38               this .thread.Abort();
39          }
40           // 获取线程状态
41           public   string  GetThreadState()
42          {
43               return  thread.ThreadState.ToString();
44          }
45           // 获取线程第几次执行
46           public   string  GetValue()
47          {
48               return  value;
49          }
50      }
51 

 

 再写用户开启多线程跟关闭,管理的具体方法

 

操作代码
 1  protected   void  open_Click( object  sender, EventArgs e)
 2          {
 3               // 定义一个哈希表
 4              Hashtable ht  =   new  Hashtable();
 5               // session是要存哈希用的
 6               if  (Session[ " thread " ]  !=   null )
 7              {
 8                   try
 9                  {
10                       // 实例化并开启线程
11                      work w  =   null ;
12                       if  (tb.Text.Trim()  !=   "" )
13                          w  =   new  work(tb.Text.Trim());
14                      w.start();
15 
16                       // 将已经存入session中的哈希信息传给ht
17                      ht  =  (Hashtable)Session[ " thread " ];
18                       // 将线程存入哈希表中
19                      ht.Add(tb.Text.Trim(), w);
20                       // 打印线程信息
21                      PrintThread();
22                      HttpContext.Current.Response.Write( " <script>alert('线程创建成功');</script> " );
23                  }
24                   catch (Exception ex)
25                  {
26                      HttpContext.Current.Response.Write( " <script>alert('来自创建有session: "   +  ex.Message  +   " ');</script> " );
27                  }
28              }
29               else
30              {
31                   try
32                  {
33                      work w  =   null ;
34                       if  (tb.Text.Trim()  !=   "" )
35                          w  =   new  work(tb.Text.Trim());
36                      w.start();
37                      
38                      ht.Add(tb.Text.Trim(), w);
39                      Session[ " thread " ]  =  ht;
40                      PrintThread();
41                  }
42                   catch  (Exception ex)
43                  {
44                      HttpContext.Current.Response.Write( " <script>alert('来自创建无session "   +  ex.Message  +   " ');</script> " );
45                  }
46              }
47          }

 

关闭线程

关闭代码
 1  // 关闭线程
 2           protected   void  close_Click( object  sender, EventArgs e)
 3          {
 4               if  (Session[ " thread " ]  ==   null )
 5                  HttpContext.Current.Response.Write( " <script>alert('对不起,线程为空');</script> " );
 6               else
 7              {
 8                   try
 9                  {
10                       // 将线程哈希取出
11                      Hashtable ht  =  (Hashtable)Session[ " thread " ];
12                       // 停止线程
13                      work w  =  (work)ht[tb.Text.Trim()];
14                      w.stop();
15                      w  =   null ;
16                      ht.Remove(tb.Text.Trim());
17                       // 打印信息
18                      PrintThread();
19                      HttpContext.Current.Response.Write( " <script>alert('线程 "   +  tb.Text.Trim()  +   " 已关闭');</script> " );
20                  }
21                   catch  (Exception ex)
22                  {
23                      HttpContext.Current.Response.Write( " <script>alert(' "   +  ex.Message  +   " ');</script> " );
24                  }
25              }
26          }

 

此方法是将多线程存入到hashtable中进行管理,而不是用线程池.

转载于:https://www.cnblogs.com/cha1r/p/3472285.html

你可能感兴趣的文章
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>