稳扎稳打Silverlight(26) - 2.0线程之Lock,Interlocked,EventWaitHandle,Monitor,ThreadStaticAttribute
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
namespace
Silverlight20.Thread
{
public partial class Lock : UserControl
{
@H_635_301@//@H_635_301@ 需要被 lock 的静态变量@H_635_
301@
private static readonly object objLock = new object();
private static int i;
public Lock()
{
initializeComponent();
i = @H_355_404@0;
for (int x = @H_355_
404@0
; x < @H_355_
404@100
; x++)
@H_197_537@
{
@H_635_301@//@H_635_
301@ 开 100 个线程去操作静态变量 i@H_635_
301@
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
System.Threading.Thread.Sleep(@H_355_
404@3000
);
@H_635_
301@//@H_635_
301@ 3 秒后 100 个线程都应该执行完毕了,取得 i 的结果
@H_635_
301@//@H_635_
301@
做了并发处理的结果为 100 ,去掉 lock 可得到不做并发处理的结果@H_635_
301@
txtmsg.Text = i.ToString();
}
private void DoWork()
@H_154_
674@
{
try
@H_128_696@
{
@H_635_301@//@H_635_301@ lock() - 确保代码块完成运行,而不会被其他线程中断。其参数必须为一个引用类型的对象@H_635_
301@
lock (objLock)
{
int j = i + @H_355_404@1;
@H_635_
301@//@H_635_
301@ 模拟多线程并发操作静态变量 i 的情况@H_635_
301@
System.Threading.Thread.Sleep(@H_355_
404@10
);
i = j;
}
}
finally
{
@H_635_301@//@H_635_
301@ code@H_635_
301@
}
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
namespace
Silverlight20.Thread
{
public partial class Interlocked : UserControl
{
private static int i;
public Interlocked()
{
initializeComponent();
i = @H_355_404@0;
for (int x = @H_355_404@0; x < @H_355_404@100; x++)
{
@H_635_301@//@H_635_
301@ 开 100 个线程去操作静态变量 i@H_635_
301@
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
System.Threading.Thread.Sleep(@H_355_
404@1000
);
@H_635_
301@//@H_635_
301@ 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果@H_635_
301@
txtmsg.Text = i.ToString();
}
private void DoWork()
@H_792_1
489@
{
try
{
@H_635_301@//@H_635_301@ Interlocked - 为多个线程共享的变量提供原子级的操作(避免并发问题)
@H_635_
301@//@H_635_
301@ i 加 1@H_635_
301@
System.Threading.Interlocked.Increment(ref i);
@H_635_
301@//@H_635_
301@ i 减 1@H_635_
301@
System.Threading.Interlocked.Decrement(ref i);
@H_635_
301@//@H_635_
301@ i 加 1@H_635_
301@
System.Threading.Interlocked.Add(ref i, @H_355_
404@1
);
@H_635_
301@//@H_635_
301@ 如果 i 等于 100 ,则将 i 赋值为 101@H_635_
301@
System.Threading.Interlocked.CompareExchange(ref i, @H_355_
404@101
, @H_355_
404@100
);
@H_635_
301@//@H_635_
301@ 将 i 赋值为 1000
@H_635_
301@//@H_635_
301@ Sy
stem.Threading.Interlocked.Exchange(ref i, 1000
);@H_635_
301@
}
finally
@H_345_1
696@
{
@H_635_
301@//@H_635_
301@ code@H_635_
301@
}
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
namespace
Silverlight20.Thread
@H_801_2043@
{
public partial class EventWaitHandle : UserControl
{
@H_635_301@//@H_635_301@ AutoResetEvent(bool statE) - 通知其他线程是否可入的类,自动 reset()
@H_635_
301@//@H_635_
301@ bool state - 是否为终止状态,即是否
禁止其他线程入内@H_635_
301@
private System.Threading.AutoResetEvent autoResetEvent =
new System.Threading.AutoResetEvent(false);
@H_635_
301@//@H_635_
301@ ManualResetEvent(bool stat
E) -
通知其他线程是否可入的类,手动
reset()
@H_635_
301@//@H_635_
301@ bool state - 是否为终止状态,即是否
禁止其他线程入内@H_635_
301@
private System.Threading.ManualResetEvent manualResetEvent =
new System.Threading.ManualResetEvent(false);
private static int i;
public EventWaitHandle()
{
initializeComponent();
@H_635_301@//@H_635_
301@ 演示 AutoResetEvent@H_635_
301@
AutoResetEventDemo();
@H_635_
301@//@H_635_
301@ 演示 ManualResetEvent@H_635_
301@
ManualResetEventDemo();
}
private void AutoResetEventDemo()
@H_174_2331@
{
i = @H_355_404@0;
for (int x = @H_355_
404@0
; x < @H_355_
404@100
; x++)
{
@H_635_301@//@H_635_
301@ 开 100 个线程去操作静态变量 i@H_635_
301@
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(AutoResetEventDemoCallBACk));
thread.Start();
@H_635_
301@//@H_635_
301@ 阻塞当前线程,直到 AutoResetEvent 发出 Set() 信号@H_635_
301@
autoResetEvent.WaitOne();
}
System.Threading.Thread.Sleep(@H_355_
404@1000
);
@H_635_
301@//@H_635_
301@ 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果@H_635_
301@
txtAutoResetEvent.Text = i.ToString();
}
private void AutoResetEventDemoCallBACk()
{
try
{
int j = i + @H_355_404@1;
@H_635_301@//@H_635_
301@ 模拟多线程并发操作静态变量 i 的情况@H_635_
301@
System.Threading.Thread.Sleep(@H_355_
404@5
);
i = j;
}
finally
{
@H_635_301@//@H_635_
301@ 发出 Set() 信号,以释放 AutoResetEvent 所阻塞的线程@H_635_
301@
autoResetEvent.Set();
}
}
private void ManualResetEventDemo()
{
i = @H_355_404@0;
for (int x = @H_355_
404@0
; x < @H_355_
404@100
; x++)
{
@H_635_301@//@H_635_
301@
reset() - 将 ManualResetEvent 变为非终止状态,即由此线程控制 ManualResetEvent,
@H_635_
301@//@H_635_
301@ 其他线程排队,直到 ManualResetEvent 发出 Set() 信号(AutoResetEvent 在 Set() 时会
自动 reset())@H_635_
301@
manualResetEvent.Reset();
@H_635_
301@//@H_635_
301@ 开 100 个线程去操作静态变量 i@H_635_
301@
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(ManualResetEventDemoCallBACk));
thread.Start();
@H_635_
301@//@H_635_
301@ 阻塞当前线程,直到 ManualResetEvent 发出 Set() 信号@H_635_
301@
manualResetEvent.WaitOne();
}
System.Threading.Thread.Sleep(@H_355_
404@1000
);
@H_635_
301@//@H_635_
301@ 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果@H_635_
301@
txtManualResetEvent.Text = i.ToString();
}
private void ManualResetEventDemoCallBACk()
{
try
{
int j = i + @H_355_404@1;
@H_635_301@//@H_635_
301@ 模拟多线程并发操作静态变量 i 的情况@H_635_
301@
System.Threading.Thread.Sleep(@H_355_
404@5
);
i = j;
}
finally
{
@H_635_301@//@H_635_
301@ 发出 Set() 信号,以释放 ManualResetEvent 所阻塞的线程,同时 ManualResetEvent 变为终止状态)@H_635_
301@
manualResetEvent.Set();
}
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
namespace
Silverlight20.Thread
{
public partial class Monitor : UserControl
@H_989_3018@
{
private static readonly object objLock = new object();
private static int i;
public Monitor()
{
initializeComponent();
i = @H_355_404@0;
for (int x = @H_355_404@0; x < @H_355_404@100; x++)
{
@H_635_301@//@H_635_
301@ 开 100 个线程去操作静态变量 i@H_635_
301@
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
System.Threading.Thread.Sleep(@H_355_
404@1000
);
@H_635_
301@//@H_635_
301@ 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果@H_635_
301@
txtmsg.Text = i.ToString();
}
private void DoWork()
@H_
618_3821@
{
try
{
@H_635_301@//@H_635_
301@ Monitor - 提供同步访问对象的机制
@H_635_
301@//@H_635_
301@ Enter() - 在指定对象上
获取排他锁@H_635_
301@
System.Threading.Monitor.Enter(objLock);
int j = i + @H_355_
404@1
;
@H_635_
301@//@H_635_
301@ 模拟多线程并发操作静态变量 i 的情况@H_635_
301@
System.Threading.Thread.Sleep(@H_355_
404@5
);
i = j;
@H_635_
301@//@H_635_
301@ Exit() - 释放指定对象上的排他锁@H_635_
301@
System.Threading.Monitor.Exit(objLock);
}
finally
{
@H_635_301@//@H_635_
301@ code@H_635_
301@
}
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
namespace
Silverlight20.Thread
{
public partial class ThreadStaticAttribute : UserControl
{
@H_635_301@//@H_635_301@ ThreadStatic - 所指定的静态变量对每个线程都是唯一的@H_635_
301@
[System.ThreadStatic]
private static int value;
@H_635_
301@//@H_635_
301@
一般的静态变量,对每个线程都是共用的@H_635_
301@
private static int value2;
public ThreadStaticAttribute()
@H_189_4489@
@H_256_
4500@
{
initializeComponent();
Demo();
}
void Demo()
{
System.Threading.Thread thread = new System.Threading.Thread(DoWork);
thread.Name = "线程1";
thread.Start();
System.Threading.Thread.Sleep(@H_355_404@100);
System.Threading.Thread thread2 = new System.Threading.Thread(DoWork2);
thread2.Name = "线程2";
thread2.Start();
}
void DoWork()
@H_424_4696@
@H_
607_4705@
{
for (int i = @H_355_404@0; i < @H_355_
404@10
; i++)
{
@H_635_301@//@H_635_
301@ 线程1对静态变量的操作@H_635_
301@
value++;
value2++;
}
String s = value.ToString(); @H_635_
301@//@H_635_
301@ value - 本线程独有的静态变量@H_635_
301@
String s2 = value2.ToString(); @H_635_
301@//@H_635_
301@ value2 - 所有线程共用的静态变量@H_635_
301@
this.Dispatcher.beginInvoke(delegate @H_621_4
874@
{ txtmsg.Text = s + " - " + s2; });
@H_635_
301@//@H_635_
301@ thi
s.Dispatcher
.beginInvoke(delegate
{ tx
tmsg.Text = value + " - " + value2; }
); @H_635_
301@//@H_635_
301@ 在UI线程上
调用,所以value值为UI线程上的value值,即 0 @H_635_
301@
}
void DoWork2()
{
for (int i = @H_355_404@0; i < @H_355_
404@10
; i++)
{
@H_635_301@//@H_635_
301@ 线程2对静态变量的操作@H_635_
301@
value++;
value2++;
}
String s = value.ToString(); @H_635_
301@//@H_635_
301@ value - 本线程独有的静态变量@H_635_
301@
String s2 = value2.ToString(); @H_635_
301@//@H_635_
301@ value2 - 所有线程共用的静态变量@H_635_
301@
@H_618_5093@
this.Dispatcher.beginInvoke(delegate @H_721_4
403@
{ txtmsg2.Text = s + " - " + s2; });
@H_635_
301@//@H_635_
301@ thi
s.Dispatcher
.beginInvoke(delegate
{ tx
tmsg2.Text = value + " - " + value2; }
); @H_635_
301@//@H_635_
301@ 在UI线程上
调用,所以value值为UI线程上的value值,即 0 @H_635_
301@
}
}
}