
[源码下载]
稳扎稳打Silverlight(26) - 2.0线程之Lock,Interlocked,EventWaitHandle,Monitor,ThreadStaticAttribute
作者: webabcd
介绍
Silverlight 2.0 使用Lock,Monitor来实现线程同步
Lock - 确保代码块完成运行,而不会被其他线程中断
Interlocked - 为多个线程共享的变量提供原子级的 *** 作
EventWaitHandle - 通知其他线程是否可入的类
Monitor - 提供同步访问对象的机制
ThreadStaticAttribute - 所指定的静态变量对每个线程都是唯一的
在线DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
示例
1、Lock.xaml
< UserControl x:Class ="Silverlight20.Thread.Lock"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< StackPanel HorizontalAlignment ="left" margin ="5" >
< TextBlock x:name ="txtMsg" />
</ StackPanel >
</ UserControl >
Lock.xaml.cs
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_480_301@//@H_480_301@ 需要被 lock 的静态变量@H_480_301@
private static Readonly object objLock = new object();
private static int i;
public Lock()
{
InitializeComponent();
i = @H_390_404@0;
for (int x = @H_390_404@0; x < @H_390_404@100; x++)
{
@H_480_301@//@H_480_301@ 开 100 个线程去 *** 作静态变量 i@H_480_301@
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
System.Threading.Thread.Sleep(@H_390_404@3000);
@H_480_301@//@H_480_301@ 3 秒后 100 个线程都应该执行完毕了,取得 i 的结果
@H_480_301@//@H_480_301@ 做了并发处理的结果为 100 ,去掉 lock 可得到不做并发处理的结果@H_480_301@
txtMsg.Text = i.ToString();
}
private voID DoWork()
{
try
{
@H_480_301@//@H_480_301@ lock() - 确保代码块完成运行,而不会被其他线程中断。其参数必须为一个引用类型的对象@H_480_301@
lock (objLock)
{
int j = i + @H_390_404@1;
@H_480_301@//@H_480_301@ 模拟多线程并发 *** 作静态变量 i 的情况@H_480_301@
System.Threading.Thread.Sleep(@H_390_404@10);
i = j;
}
}
finally
{
@H_480_301@//@H_480_301@ code@H_480_301@
}
}
}
}
2、Interlocked.xaml
< UserControl x:Class ="Silverlight20.Thread.Interlocked"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< StackPanel HorizontalAlignment ="left" margin ="5" >
< TextBlock x:name ="txtMsg" />
</ StackPanel >
</ UserControl >
Interlocked.xaml.cs
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_390_404@0;
for (int x = @H_390_404@0; x < @H_390_404@100; x++)
{
@H_480_301@//@H_480_301@ 开 100 个线程去 *** 作静态变量 i@H_480_301@
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
System.Threading.Thread.Sleep(@H_390_404@1000);
@H_480_301@//@H_480_301@ 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果@H_480_301@
txtMsg.Text = i.ToString();
}
private voID DoWork()
{
try
{
@H_480_301@//@H_480_301@ Interlocked - 为多个线程共享的变量提供原子级的 *** 作(避免并发问题)
@H_480_301@//@H_480_301@ i 加 1@H_480_301@
System.Threading.Interlocked.Increment(ref i);
@H_480_301@//@H_480_301@ i 减 1@H_480_301@
System.Threading.Interlocked.Decrement(ref i);
@H_480_301@//@H_480_301@ i 加 1@H_480_301@
System.Threading.Interlocked.Add(ref i, @H_390_404@1);
@H_480_301@//@H_480_301@ 如果 i 等于 100 ,则将 i 赋值为 101@H_480_301@
System.Threading.Interlocked.CompareExchange(ref i, @H_390_404@101, @H_390_404@100);
@H_480_301@//@H_480_301@ 将 i 赋值为 1000
@H_480_301@//@H_480_301@ System.Threading.Interlocked.Exchange(ref i, 1000);@H_480_301@
}
finally
{
@H_480_301@//@H_480_301@ code@H_480_301@
}
}
}
}
3、EventWaitHandle.xaml
< UserControl x:Class ="Silverlight20.Thread.EventWaitHandle"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< StackPanel HorizontalAlignment ="left" margin ="5" >
< TextBlock x:name ="txtautoresetEvent" />
< TextBlock x:name ="txtManualresetEvent" />
</ StackPanel >
</ UserControl >
EventWaitHandle.xaml.cs
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 EventWaitHandle : UserControl
{
@H_480_301@//@H_480_301@ autoresetEvent(bool state) - 通知其他线程是否可入的类,自动 reset()
@H_480_301@//@H_480_301@ bool state - 是否为终止状态,即是否禁止其他线程入内@H_480_301@
private System.Threading.autoresetEvent autoresetEvent =
new System.Threading.autoresetEvent(false);
@H_480_301@//@H_480_301@ ManualresetEvent(bool state) - 通知其他线程是否可入的类,手动 reset()
@H_480_301@//@H_480_301@ bool state - 是否为终止状态,即是否禁止其他线程入内@H_480_301@
private System.Threading.ManualresetEvent manualresetEvent =
new System.Threading.ManualresetEvent(false);
private static int i;
public EventWaitHandle()
{
InitializeComponent();
@H_480_301@//@H_480_301@ 演示 autoresetEvent@H_480_301@
autoresetEventDemo();
@H_480_301@//@H_480_301@ 演示 ManualresetEvent@H_480_301@
ManualresetEventDemo();
}
private voID autoresetEventDemo()
{
i = @H_390_404@0;
for (int x = @H_390_404@0; x < @H_390_404@100; x++)
{
@H_480_301@//@H_480_301@ 开 100 个线程去 *** 作静态变量 i@H_480_301@
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(autoresetEventDemoCallback));
thread.Start();
@H_480_301@//@H_480_301@ 阻塞当前线程,直到 autoresetEvent 发出 Set() 信号@H_480_301@
autoresetEvent.WaitOne();
}
System.Threading.Thread.Sleep(@H_390_404@1000);
@H_480_301@//@H_480_301@ 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果@H_480_301@
txtautoresetEvent.Text = i.ToString();
}
private voID autoresetEventDemoCallback()
{
try
@H_403_2223@{
int j = i + @H_390_404@1;
@H_480_301@//@H_480_301@ 模拟多线程并发 *** 作静态变量 i 的情况@H_480_301@
System.Threading.Thread.Sleep(@H_390_404@5);
i = j;
}
finally
{
@H_480_301@//@H_480_301@ 发出 Set() 信号,以释放 autoresetEvent 所阻塞的线程@H_480_301@
autoresetEvent.Set();
}
}
private voID ManualresetEventDemo()
{
i = @H_390_404@0;
for (int x = @H_390_404@0; x < @H_390_404@100; x++)
{
@H_480_301@//@H_480_301@ reset() - 将 ManualresetEvent 变为非终止状态,即由此线程控制 ManualresetEvent,
@H_480_301@//@H_480_301@ 其他线程排队,直到 ManualresetEvent 发出 Set() 信号(autoresetEvent 在 Set() 时会自动 reset())@H_480_301@
manualresetEvent.reset();
@H_480_301@//@H_480_301@ 开 100 个线程去 *** 作静态变量 i@H_480_301@
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(ManualresetEventDemoCallback));
thread.Start();
@H_480_301@//@H_480_301@ 阻塞当前线程,直到 ManualresetEvent 发出 Set() 信号@H_480_301@
manualresetEvent.WaitOne();
}
System.Threading.Thread.Sleep(@H_390_404@1000);
@H_480_301@//@H_480_301@ 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果@H_480_301@
txtManualresetEvent.Text = i.ToString();
}
private voID ManualresetEventDemoCallback()
{
try
{
int j = i + @H_390_404@1;
@H_480_301@//@H_480_301@ 模拟多线程并发 *** 作静态变量 i 的情况@H_480_301@
System.Threading.Thread.Sleep(@H_390_404@5);
i = j;
}
finally
{
@H_480_301@//@H_480_301@ 发出 Set() 信号,以释放 ManualresetEvent 所阻塞的线程,同时 ManualresetEvent 变为终止状态)@H_480_301@
manualresetEvent.Set();
}
}
}
}
4、Monitor.xaml
< UserControl x:Class ="Silverlight20.Thread.Monitor"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< StackPanel HorizontalAlignment ="left" margin ="5" >
< TextBlock x:name ="txtMsg" />
</ StackPanel >
</ UserControl >
Monitor.xaml.cs
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_844_3018@
{private static Readonly object objLock = new object();
private static int i;
public Monitor()
{
InitializeComponent();
i = @H_390_404@0;
for (int x = @H_390_404@0; x < @H_390_404@100; x++)
{
@H_480_301@//@H_480_301@ 开 100 个线程去 *** 作静态变量 i@H_480_301@
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
System.Threading.Thread.Sleep(@H_390_404@1000);
@H_480_301@//@H_480_301@ 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果@H_480_301@
txtMsg.Text = i.ToString();
}
private voID DoWork()
{
try
{
@H_480_301@//@H_480_301@ Monitor - 提供同步访问对象的机制
@H_480_301@//@H_480_301@ Enter() - 在指定对象上获取排他锁@H_480_301@
System.Threading.Monitor.Enter(objLock);
int j = i + @H_390_404@1;
@H_480_301@//@H_480_301@ 模拟多线程并发 *** 作静态变量 i 的情况@H_480_301@
System.Threading.Thread.Sleep(@H_390_404@5);
i = j;
@H_480_301@//@H_480_301@ Exit() - 释放指定对象上的排他锁@H_480_301@
System.Threading.Monitor.Exit(objLock);
}
finally
{
@H_480_301@//@H_480_301@ code@H_480_301@
}
}
}
}
5、ThreadStaticAttribute.xaml
< UserControl x:Class ="Silverlight20.Thread.ThreadStaticAttribute"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< StackPanel HorizontalAlignment ="left" margin ="5" >
< TextBlock x:name ="txtMsg" />
< TextBlock x:name ="txtMsg2" />
</ StackPanel >
</ UserControl >
ThreadStaticAttribute.xaml.cs
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_480_301@//@H_480_301@ ThreadStatic - 所指定的静态变量对每个线程都是唯一的@H_480_301@
[System.ThreadStatic]
private static int value;
@H_480_301@//@H_480_301@ 一般的静态变量,对每个线程都是共用的@H_480_301@
private static int value2;
public ThreadStaticAttribute()
{
InitializeComponent();
Demo();
}
voID Demo()
{
System.Threading.Thread thread = new System.Threading.Thread(DoWork);
thread.name = "线程1";
thread.Start();
System.Threading.Thread.Sleep(@H_390_404@100);
System.Threading.Thread thread2 = new System.Threading.Thread(DoWork2);
thread2.name = "线程2";
thread2.Start();
}
voID DoWork()
{
for (int i = @H_390_404@0; i < @H_390_404@10; i++)
{
@H_480_301@//@H_480_301@ 线程1对静态变量的 *** 作@H_480_301@
value++;
value2++;
}
string s = value.ToString(); @H_480_301@//@H_480_301@ value - 本线程独有的静态变量@H_480_301@
string s2 = value2.ToString(); @H_480_301@//@H_480_301@ value2 - 所有线程共用的静态变量@H_480_301@
this.dispatcher.BeginInvoke(delegate { txtMsg.Text = s + " - " + s2; });
@H_480_301@//@H_480_301@ this.dispatcher.BeginInvoke(delegate { txtMsg.Text = value + " - " + value2; }); @H_480_301@//@H_480_301@ 在UI线程上调用,所以value值为UI线程上的value值,即 0 @H_480_301@
}
voID DoWork2()
{
for (int i = @H_390_404@0; i < @H_390_404@10; i++)
{
@H_480_301@//@H_480_301@ 线程2对静态变量的 *** 作@H_480_301@
value++;
value2++;
}
string s = value.ToString(); @H_480_301@//@H_480_301@ value - 本线程独有的静态变量@H_480_301@
string s2 = value2.ToString(); @H_480_301@//@H_480_301@ value2 - 所有线程共用的静态变量@H_480_301@
this.dispatcher.BeginInvoke(delegate @H_688_4403@{ txtMsg2.Text = s + " - " + s2; });
@H_480_301@//@H_480_301@ this.dispatcher.BeginInvoke(delegate { txtMsg2.Text = value + " - " + value2; }); @H_480_301@//@H_480_301@ 在UI线程上调用,所以value值为UI线程上的value值,即 0 @H_480_301@
}
}
}
OK
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor, ThreadStaticAttribute全部内容,希望文章能够帮你解决稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor, ThreadStaticAttribute所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)