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() );
}
}
}
}
}
查看全文
相关阅读:
一种 动态 样式 语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, Firefox),也可以借助Node.js或者Rhino在服务端运行。
关于jquery选择器的一些注意
几种序列化协议(protobuf,xstream,jackjson,jdk,hessian)相关数据对比
测试:你自认为理解了JavaScript?
如何移除相邻两个 display: inlineblock 元素间的间隔
Linux TCP 连接数修改
CSS lineheight 和 verticalalign 精解(上篇)_依然梦想_百度空间
pageX,clientX,offsetX,layerX的那些事
在html5下 关于img与div之间的缝隙
iframe自适应高度详解(希望对大家有用)
原文地址:https://www.cnblogs.com/furenjun/p/349208.html
最新文章
改变你一生的理财视频教程
今天完成了C#3.0的培训课程,可以休息一下了.
Table Variable vs. Temp Table
ASP.NET 2.0 多个ValidationGroup不支持
关于解决数据库修改排序规则
学到两个SQL函数,记下来以备后用。
.NET 7期 11月上旬教学计划
多条件的数据查询,究竟该如何写SQL(兼答复5期同学彭丰华)
Neeps数据库问题
湘恒教学暂行标准【所有老师、助教必读】
热门文章
.NET 7期 11月下旬教学计划【11月18日更新】
忏悔帖:一张发票引发的血案
.NET 7期 C#入门习题【实在很简单,N多人挂了】
.NET 6期 11月下旬教学计划【11月18日更新】
HTML5本地存储不完全指南
canvas学习
关于tomcat并发
tomcat中的server.xml中线程参数:maxThreads,maxSpareThreads,这两个参数的区别,哪个值更大?
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object异常原因
jboss会议
Copyright © 2011-2022 走看看