zoukankan
html css js c++ java
简单的多线程操作示例
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
using
System.Threading;
namespace
多线程操作
{
/**/
///
<summary>
///
Form1 的摘要说明。
///
</summary>
public
class
Form1 : System.Windows.Forms.Form
{
private
System.Windows.Forms.Panel panel1;
private
System.Windows.Forms.StatusBar statusBar1;
private
System.Windows.Forms.Panel panel2;
private
System.Windows.Forms.Button button1;
private
System.Windows.Forms.RichTextBox myRichTextBox;
private
System.Windows.Forms.Button button2;
private
System.Windows.Forms.Button button3;
private
System.Windows.Forms.Button button4;
private
System.Windows.Forms.Button button5;
private
System.Windows.Forms.Button button6;
/**/
///
<summary>
///
必需的设计器变量。
///
</summary>
private
System.ComponentModel.Container components
=
null
;
public
Form1()
{
//
//
Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
//
TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**/
///
<summary>
///
清理所有正在使用的资源。
///
</summary>
protected
override
void
Dispose(
bool
disposing )
{
if
( disposing )
{
if
(components
!=
null
)
{
components.Dispose();
}
}
base
.Dispose( disposing );
}
Windows 窗体设计器生成的代码
#region
Windows 窗体设计器生成的代码
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.panel1
=
new
System.Windows.Forms.Panel();
this
.statusBar1
=
new
System.Windows.Forms.StatusBar();
this
.panel2
=
new
System.Windows.Forms.Panel();
this
.myRichTextBox
=
new
System.Windows.Forms.RichTextBox();
this
.button1
=
new
System.Windows.Forms.Button();
this
.button2
=
new
System.Windows.Forms.Button();
this
.button3
=
new
System.Windows.Forms.Button();
this
.button4
=
new
System.Windows.Forms.Button();
this
.button5
=
new
System.Windows.Forms.Button();
this
.button6
=
new
System.Windows.Forms.Button();
this
.panel2.SuspendLayout();
this
.SuspendLayout();
//
//
panel1
//
this
.panel1.Dock
=
System.Windows.Forms.DockStyle.Top;
this
.panel1.Location
=
new
System.Drawing.Point(
0
,
0
);
this
.panel1.Name
=
"
panel1
"
;
this
.panel1.Size
=
new
System.Drawing.Size(
696
,
32
);
this
.panel1.TabIndex
=
1
;
//
//
statusBar1
//
this
.statusBar1.Location
=
new
System.Drawing.Point(
0
,
392
);
this
.statusBar1.Name
=
"
statusBar1
"
;
this
.statusBar1.ShowPanels
=
true
;
this
.statusBar1.Size
=
new
System.Drawing.Size(
696
,
22
);
this
.statusBar1.TabIndex
=
2
;
//
//
panel2
//
this
.panel2.Controls.Add(
this
.button3);
this
.panel2.Controls.Add(
this
.button2);
this
.panel2.Controls.Add(
this
.button1);
this
.panel2.Controls.Add(
this
.button4);
this
.panel2.Controls.Add(
this
.button5);
this
.panel2.Controls.Add(
this
.button6);
this
.panel2.Dock
=
System.Windows.Forms.DockStyle.Right;
this
.panel2.Location
=
new
System.Drawing.Point(
544
,
32
);
this
.panel2.Name
=
"
panel2
"
;
this
.panel2.Size
=
new
System.Drawing.Size(
152
,
360
);
this
.panel2.TabIndex
=
3
;
//
//
myRichTextBox
//
this
.myRichTextBox.Dock
=
System.Windows.Forms.DockStyle.Fill;
this
.myRichTextBox.Location
=
new
System.Drawing.Point(
0
,
32
);
this
.myRichTextBox.Name
=
"
myRichTextBox
"
;
this
.myRichTextBox.Size
=
new
System.Drawing.Size(
544
,
360
);
this
.myRichTextBox.TabIndex
=
4
;
this
.myRichTextBox.Text
=
""
;
//
//
button1
//
this
.button1.Location
=
new
System.Drawing.Point(
16
,
16
);
this
.button1.Name
=
"
button1
"
;
this
.button1.Size
=
new
System.Drawing.Size(
120
,
24
);
this
.button1.TabIndex
=
0
;
this
.button1.Text
=
"
双线程
"
;
this
.button1.Click
+=
new
System.EventHandler(
this
.button1_Click);
//
//
button2
//
this
.button2.Location
=
new
System.Drawing.Point(
16
,
48
);
this
.button2.Name
=
"
button2
"
;
this
.button2.Size
=
new
System.Drawing.Size(
120
,
23
);
this
.button2.TabIndex
=
1
;
this
.button2.Text
=
"
调其它类中的线程式
"
;
this
.button2.Click
+=
new
System.EventHandler(
this
.button2_Click);
//
//
button3
//
this
.button3.Location
=
new
System.Drawing.Point(
16
,
80
);
this
.button3.Name
=
"
button3
"
;
this
.button3.Size
=
new
System.Drawing.Size(
120
,
24
);
this
.button3.TabIndex
=
2
;
this
.button3.Text
=
"
线程休眠
"
;
this
.button3.Click
+=
new
System.EventHandler(
this
.button3_Click);
//
//
button4
//
this
.button4.Location
=
new
System.Drawing.Point(
16
,
112
);
this
.button4.Name
=
"
button4
"
;
this
.button4.Size
=
new
System.Drawing.Size(
120
,
24
);
this
.button4.TabIndex
=
2
;
this
.button4.Text
=
"
设定为后台线程
"
;
this
.button4.Click
+=
new
System.EventHandler(
this
.button4_Click);
//
//
button5
//
this
.button5.Location
=
new
System.Drawing.Point(
16
,
152
);
this
.button5.Name
=
"
button5
"
;
this
.button5.Size
=
new
System.Drawing.Size(
120
,
24
);
this
.button5.TabIndex
=
2
;
this
.button5.Text
=
"
设定优先级和阻塞调用
"
;
this
.button5.Click
+=
new
System.EventHandler(
this
.button5_Click);
//
//
button6
//
this
.button6.Location
=
new
System.Drawing.Point(
16
,
192
);
this
.button6.Name
=
"
button6
"
;
this
.button6.Size
=
new
System.Drawing.Size(
120
,
24
);
this
.button6.TabIndex
=
2
;
this
.button6.Text
=
"
设定阻塞调用时间
"
;
this
.button6.Click
+=
new
System.EventHandler(
this
.button6_Click);
//
//
Form1
//
this
.AutoScaleBaseSize
=
new
System.Drawing.Size(
6
,
14
);
this
.ClientSize
=
new
System.Drawing.Size(
696
,
414
);
this
.Controls.Add(
this
.myRichTextBox);
this
.Controls.Add(
this
.panel2);
this
.Controls.Add(
this
.statusBar1);
this
.Controls.Add(
this
.panel1);
this
.Name
=
"
Form1
"
;
this
.Text
=
"
Form1
"
;
this
.Load
+=
new
System.EventHandler(
this
.Form1_Load);
this
.panel2.ResumeLayout(
false
);
this
.ResumeLayout(
false
);
}
#endregion
/**/
///
<summary>
///
应用程序的主入口点。
///
</summary>
[STAThread]
static
void
Main()
{
Application.Run(
new
Form1());
}
private
void
Form1_Load(
object
sender, System.EventArgs e)
{
}
# region
"
button1
"
//
public static void Thread1()
public
void
Thread1()
{
for
(
int
i
=
0
; i
<
10
; i
++
)
{
this
.myRichTextBox.Text
+=
String.Format(
"
\nThread1 {0}
"
, i);
}
}
//
public static void Thread2()
public
void
Thread2()
{
for
(
int
i
=
0
; i
<
10
; i
++
)
{
this
.myRichTextBox.Text
+=
String.Format(
"
\nThread2 {0}
"
, i);
}
}
private
void
button1_Click(
object
sender, System.EventArgs e)
{
this
.myRichTextBox.Text
=
""
;
Thread tid1
=
new
Thread(
new
ThreadStart(Thread1 ) );
Thread tid2
=
new
Thread(
new
ThreadStart(Thread2 ) );
tid1.Start();
tid2.Start();
}
#endregion
"button2"
#region
"button2"
private
void
button2_Click(
object
sender, System.EventArgs e)
{
execThread();
}
private
static
void
execThread()
{
Thread tid1
=
new
Thread(
new
ThreadStart(MyThread.Thread1 ) );
Thread tid2
=
new
Thread(
new
ThreadStart(MyThread.Thread2 ) );
tid1.Start();
tid2.Start();
}
# endregion
"线程休眠"
#region
"线程休眠"
private
void
button3_Click(
object
sender, System.EventArgs e)
{
//
Thread tid1 = new Thread(new ThreadStart(Thread3) );
//
Thread tid2 = new Thread(new ThreadStart(Thread3) );
//
//
tid1.Start();
//
tid2.Start();
//
定义成static 型后将不会出现前面线程执行的情况
execThread2();
//
休眠一秒钟
}
private
static
void
execThread2()
{
MyThread thr1
=
new
MyThread();
MyThread thr2
=
new
MyThread();
Thread tid1
=
new
Thread(
new
ThreadStart(thr1.Thread3) );
Thread tid2
=
new
Thread(
new
ThreadStart(thr2.Thread3) );
tid1.Name
=
"
Thread 31
"
;
tid2.Name
=
"
Thread 32
"
;
tid1.Start();
tid2.Start();
if
(MyThread.gsCr
>=
5
)
{
tid1.Start();
tid2.Start();
try
{
tid1.Abort();
tid2.Abort();
}
catch
(ThreadAbortException tae)
{
Console.WriteLine(tae.ToString() );
}
Console.WriteLine(
"
End of Main
"
);
}
}
//
设定为后台线程
private
static
void
execThread4()
{
Console.WriteLine(
"
Before start thread
"
);
MyThread thr1
=
new
MyThread();
MyThread thr2
=
new
MyThread();
Thread tid1
=
new
Thread(
new
ThreadStart(thr1.Thread3) );
Thread tid2
=
new
Thread(
new
ThreadStart(thr2.Thread3) );
tid1.Name
=
"
Thread 31
"
;
tid2.Name
=
"
Thread 32
"
;
tid1.IsBackground
=
true
;
//
是否为后台线程
tid2.IsBackground
=
true
;
try
{
tid1.Start();
tid2.Start();
}
catch
(ThreadStateException te)
{
Console.WriteLine(te.ToString() );
}
Thread.Sleep(
10
);
Console.WriteLine(
"
End of Main
"
);
}
public
void
Thread3()
{
for
(
int
i
=
0
; i
<
10
; i
++
)
{
this
.myRichTextBox.Text
+=
String.Format(
"
\nThread3 {0}
"
, i);
//
Thread.Sleep(1) ;
//
休眠一秒钟
int
iHour
=
0
;
int
iMin
=
0
;
int
iSec
=
1
;
//
-1
Thread.Sleep(
new
TimeSpan(iHour, iMin, iSec) );
}
}
#endregion
private
void
button4_Click(
object
sender, System.EventArgs e)
{
System.Windows.Forms.Timer myTimer
=
new
System.Windows.Forms.Timer();
myTimer.Interval
=
1
;
myTimer.Tick
+=
new
EventHandler(myTimer_Tick);
myTimer.Start();
execThread4();
//
设定为后台运行
MessageBox.Show(
"
运行结束
"
+
"
共用时:
"
+
iTimer.ToString() ) ;
myTimer.Stop();
}
int
iTimer
=
0
;
private
void
myTimer_Tick(
object
sender, EventArgs e)
{
iTimer
+=
1
;
}
private
static
void
execThread5()
//
设定优先级和阻塞调用
{
MyThread thr1
=
new
MyThread();
MyThread thr2
=
new
MyThread();
Thread tid1
=
new
Thread(
new
ThreadStart(thr1.Thread3) );
Thread tid2
=
new
Thread(
new
ThreadStart(thr2.Thread3) );
tid1.Name
=
"
Thread 31
"
;
tid2.Name
=
"
Thread 32
"
;
tid2.Priority
=
ThreadPriority.Highest;
//
tid2优先级高于tid1
tid1.Priority
=
ThreadPriority.Lowest;
try
{
tid1.Start();
tid2.Start();
}
catch
(ThreadStateException te)
{
Console.WriteLine(te.ToString() );
}
tid1.Join();
//
阻塞调用线程
tid2.Join();
Console.WriteLine(
"
End of Main
"
);
}
private
void
button5_Click(
object
sender, System.EventArgs e)
{
execThread5();
//
设定优先级和阻塞调用
}
private
static
void
execThread6()
{
Console.WriteLine(
"
Before start thread
"
);
MyThread thr1
=
new
MyThread();
MyThread thr2
=
new
MyThread();
Thread tid1
=
new
Thread(
new
ThreadStart(thr1.Thread3) );
Thread tid2
=
new
Thread(
new
ThreadStart(thr2.Thread3) );
tid1.Name
=
"
Thread 1
"
;
tid2.Name
=
"
Thread 2
"
;
try
{
tid1.Start();
tid2.Start();
}
catch
(ThreadStateException te)
{
Console.WriteLine(te.ToString() );
}
tid1.Join();
tid2.Join(
new
TimeSpan(
0
,
0
,
1000
) );
//
阻塞调用线程tid1,直到线程tid2终止或经过了指定的时间1000ms为止.
Console.WriteLine(
"
End of Main
"
);
}
private
void
button6_Click(
object
sender, System.EventArgs e)
{
execThread6();
}
}
public
class
MyThread
{
public
static
void
Thread1()
{
for
(
int
i
=
0
; i
<
10
; i
++
)
{
Console.WriteLine(
"
Thread1 {0}
"
, i);
}
}
public
static
void
Thread2()
{
for
(
int
i
=
0
; i
<
10
; i
++
)
{
Console.WriteLine(
"
Thread2 {0}
"
, i);
}
}
public
static
int
CurrentRun
=
0
;
public
static
int
gsCr
{
get
{
return
CurrentRun;}
set
{CurrentRun
=
value;}
}
public
void
Thread3()
{
gsCr
=
0
;
for
(
int
i
=
0
; i
<
10
; i
++
)
{
Thread thr
=
Thread.CurrentThread;
Console.WriteLine(thr.Name
+
"
=
"
+
i
+
"
当前的数据:
"
+
gsCr.ToString() );
gsCr
=
i;
try
{
//
Thread.Sleep(1) ;
//
休眠一秒钟
int
iHour
=
0
;
int
iMin
=
0
;
int
iSec
=
1
;
//
-1
Thread.Sleep(
new
TimeSpan(iHour, iMin, iSec) );
}
catch
(ArgumentException ae)
{
Console.WriteLine(ae.ToString() );
}
catch
(ThreadInterruptedException tie)
{
Console.WriteLine(tie.ToString() );
}
}
}
}
}
查看全文
相关阅读:
vue-d2admin-axios异步请求登录,先对比一下Jquery ajax, Axios, Fetch区别
beego 前后端分离登录验证
Beego没gin配置静态页面方便
beego-vue URL重定向(beego和vue前后端分离开发,beego承载vue前端分离页面部署)
Go程序员面试算法宝典-读后感2-链表
Go程序员面试算法宝典-读后感1
bee api new
golang-结构体的使用
golang-笔记2
golang-笔记1
原文地址:https://www.cnblogs.com/furenjun/p/349208.html
最新文章
MySQL高可用架构之MHA
Heartbeat+DRBD+MySQL高可用方案
MySQL 高可用架构之MMM
MySQL Cluster 日常维护
MySQL Cluster 配置详细介绍
ZooKeeper工作原理
密码安全存储与登陆的方案
内网穿透及工具使用
Python APScheduler定时任务框架
go网络传输中序列化与反序列化的几种方法
热门文章
Redis实现消息队列的4种方案
ES集群、分片、副本
ElasticSearch多种搜索方式
elasticsearch搜索原理之倒排索引
Docker安装部署ELK教程 (Elasticsearch+Kibana+Logstash+Filebeat)
关于学历与能力的感想,也给想走我这条老路的人一些经验
golang-os文件操作
vuex(用了vue就上了一条不归路的贼船)
vue-d2admin前端axio异步请求详情
golang 闭包
Copyright © 2011-2022 走看看