上接稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor

简介:


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
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Thread 
InBlock.gif
InBlock.gif         public partial  class EventWaitHandle : UserControl 
InBlock.gif        { 
InBlock.gif                 // AutoResetEvent(bool state) - 通知其他线程是否可入的类,自动 Reset() 
InBlock.gif                 //         bool state - 是否为终止状态,即是否禁止其他线程入内 
InBlock.gif                 private System.Threading.AutoResetEvent autoResetEvent =    
InBlock.gif                         new System.Threading.AutoResetEvent( false); 
InBlock.gif 
InBlock.gif                 // ManualResetEvent(bool state) - 通知其他线程是否可入的类,手动 Reset() 
InBlock.gif                 //         bool state - 是否为终止状态,即是否禁止其他线程入内 
InBlock.gif                 private System.Threading.ManualResetEvent manualResetEvent =    
InBlock.gif                         new System.Threading.ManualResetEvent( false); 
InBlock.gif 
InBlock.gif                 private  static  int i; 
InBlock.gif 
InBlock.gif                 public EventWaitHandle() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         // 演示 AutoResetEvent 
InBlock.gif                        AutoResetEventDemo(); 
InBlock.gif 
InBlock.gif                         // 演示 ManualResetEvent 
InBlock.gif                        ManualResetEventDemo(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void AutoResetEventDemo() 
InBlock.gif                { 
InBlock.gif                        i = 0; 
InBlock.gif 
InBlock.gif                         for ( int x = 0; x < 100; x++) 
InBlock.gif                        { 
InBlock.gif                                 // 开 100 个线程去操作静态变量 i 
InBlock.gif                                System.Threading.Thread thread = 
InBlock.gif                                         new System.Threading.Thread( new System.Threading.ThreadStart(AutoResetEventDemoCallback)); 
InBlock.gif                                thread.Start(); 
InBlock.gif 
InBlock.gif                                 // 阻塞当前线程,直到 AutoResetEvent 发出 Set() 信号 
InBlock.gif                                autoResetEvent.WaitOne(); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        System.Threading.Thread.Sleep(1000); 
InBlock.gif                         // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果 
InBlock.gif                        txtAutoResetEvent.Text = i.ToString(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void AutoResetEventDemoCallback() 
InBlock.gif                { 
InBlock.gif                         try 
InBlock.gif                        { 
InBlock.gif                                 int j = i + 1; 
InBlock.gif 
InBlock.gif                                 // 模拟多线程并发操作静态变量 i 的情况 
InBlock.gif                                System.Threading.Thread.Sleep(5); 
InBlock.gif 
InBlock.gif                                i = j; 
InBlock.gif                        } 
InBlock.gif                         finally 
InBlock.gif                        { 
InBlock.gif                                 // 发出 Set() 信号,以释放 AutoResetEvent 所阻塞的线程 
InBlock.gif                                autoResetEvent.Set(); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif 
InBlock.gif                 private  void ManualResetEventDemo() 
InBlock.gif                { 
InBlock.gif                        i = 0; 
InBlock.gif 
InBlock.gif                         for ( int x = 0; x < 100; x++) 
InBlock.gif                        { 
InBlock.gif                                 // Reset() - 将 ManualResetEvent 变为非终止状态,即由此线程控制 ManualResetEvent, 
InBlock.gif                                 //         其他线程排队,直到 ManualResetEvent 发出 Set() 信号(AutoResetEvent 在 Set() 时会自动 Reset()) 
InBlock.gif                                manualResetEvent.Reset(); 
InBlock.gif 
InBlock.gif                                 // 开 100 个线程去操作静态变量 i 
InBlock.gif                                System.Threading.Thread thread = 
InBlock.gif                                         new System.Threading.Thread( new System.Threading.ThreadStart(ManualResetEventDemoCallback)); 
InBlock.gif                                thread.Start(); 
InBlock.gif 
InBlock.gif                                 // 阻塞当前线程,直到 ManualResetEvent 发出 Set() 信号 
InBlock.gif                                manualResetEvent.WaitOne(); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        System.Threading.Thread.Sleep(1000); 
InBlock.gif                         // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果 
InBlock.gif                        txtManualResetEvent.Text = i.ToString(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void ManualResetEventDemoCallback() 
InBlock.gif                { 
InBlock.gif                         try 
InBlock.gif                        { 
InBlock.gif                                 int j = i + 1; 
InBlock.gif 
InBlock.gif                                 // 模拟多线程并发操作静态变量 i 的情况 
InBlock.gif                                System.Threading.Thread.Sleep(5); 
InBlock.gif 
InBlock.gif                                i = j; 
InBlock.gif                        } 
InBlock.gif                         finally 
InBlock.gif                        { 
InBlock.gif                                 // 发出 Set() 信号,以释放 ManualResetEvent 所阻塞的线程,同时 ManualResetEvent 变为终止状态) 
InBlock.gif                                manualResetEvent.Set(); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
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
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Thread 
InBlock.gif
InBlock.gif         public partial  class Monitor : UserControl 
InBlock.gif        { 
InBlock.gif                 private  static  readonly  object objLock =  new  object(); 
InBlock.gif                 private  static  int i; 
InBlock.gif                 
InBlock.gif                 public Monitor() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                        i = 0; 
InBlock.gif 
InBlock.gif                         for ( int x = 0; x < 100; x++) 
InBlock.gif                        { 
InBlock.gif                                 // 开 100 个线程去操作静态变量 i 
InBlock.gif                                System.Threading.Thread thread =  new System.Threading.Thread( new System.Threading.ThreadStart(DoWork)); 
InBlock.gif                                thread.Start(); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        System.Threading.Thread.Sleep(1000); 
InBlock.gif                         // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果 
InBlock.gif                        txtMsg.Text = i.ToString(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void DoWork() 
InBlock.gif                { 
InBlock.gif                         try 
InBlock.gif                        { 
InBlock.gif                                 // Monitor - 提供同步访问对象的机制 
InBlock.gif 
InBlock.gif                                 // Enter() - 在指定对象上获取排他锁 
InBlock.gif                                System.Threading.Monitor.Enter(objLock); 
InBlock.gif 
InBlock.gif                                 int j = i + 1; 
InBlock.gif 
InBlock.gif                                 // 模拟多线程并发操作静态变量 i 的情况 
InBlock.gif                                System.Threading.Thread.Sleep(5); 
InBlock.gif 
InBlock.gif                                i = j; 
InBlock.gif 
InBlock.gif                                 // Exit() - 释放指定对象上的排他锁 
InBlock.gif                                System.Threading.Monitor.Exit(objLock); 
InBlock.gif                        } 
InBlock.gif                         finally 
InBlock.gif                        { 
InBlock.gif                                 // code 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 

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
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Thread 
InBlock.gif
InBlock.gif         public partial  class ThreadStaticAttribute : UserControl 
InBlock.gif        { 
InBlock.gif                 // ThreadStatic - 所指定的静态变量对每个线程都是唯一的 
InBlock.gif                [System.ThreadStatic] 
InBlock.gif                 private  static  int value; 
InBlock.gif 
InBlock.gif                 // 一般的静态变量,对每个线程都是共用的 
InBlock.gif                 private  static  int value2; 
InBlock.gif 
InBlock.gif                 public ThreadStaticAttribute() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                        Demo(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void Demo() 
InBlock.gif                { 
InBlock.gif                        System.Threading.Thread thread =  new System.Threading.Thread(DoWork); 
InBlock.gif                        thread.Name =  "线程1"
InBlock.gif                        thread.Start(); 
InBlock.gif 
InBlock.gif                        System.Threading.Thread.Sleep(100); 
InBlock.gif 
InBlock.gif                        System.Threading.Thread thread2 =  new System.Threading.Thread(DoWork2); 
InBlock.gif                        thread2.Name =  "线程2"
InBlock.gif                        thread2.Start(); 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void DoWork() 
InBlock.gif                { 
InBlock.gif                         for ( int i = 0; i < 10; i++) 
InBlock.gif                        { 
InBlock.gif                                 // 线程1对静态变量的操作 
InBlock.gif                                value++; 
InBlock.gif                                value2++; 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                         string s = value.ToString();  // value - 本线程独有的静态变量 
InBlock.gif                         string s2 = value2.ToString();  // value2 - 所有线程共用的静态变量 
InBlock.gif 
InBlock.gif                         this.Dispatcher.BeginInvoke( delegate { txtMsg.Text = s +  " - " + s2; }); 
InBlock.gif                         // this.Dispatcher.BeginInvoke(delegate { txtMsg.Text = value + " - " + value2; }); // 在UI线程上调用,所以value值为UI线程上的value值,即 0    
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void DoWork2() 
InBlock.gif                { 
InBlock.gif                         for ( int i = 0; i < 10; i++) 
InBlock.gif                        { 
InBlock.gif                                 // 线程2对静态变量的操作 
InBlock.gif                                value++; 
InBlock.gif                                value2++; 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                         string s = value.ToString();  // value - 本线程独有的静态变量 
InBlock.gif                         string s2 = value2.ToString();  // value2 - 所有线程共用的静态变量 
InBlock.gif 
InBlock.gif                         this.Dispatcher.BeginInvoke( delegate { txtMsg2.Text = s +  " - " + s2; }); 
InBlock.gif                         // this.Dispatcher.BeginInvoke(delegate { txtMsg2.Text = value + " - " + value2; }); // 在UI线程上调用,所以value值为UI线程上的value值,即 0    
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 


     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/343911 ,如需转载请自行联系原作者

 
相关文章
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
5月前
|
监控 安全
并发编程系列教程(06) - 多线程之间通讯(wait、notify、sleep、Lock锁、Condition)
并发编程系列教程(06) - 多线程之间通讯(wait、notify、sleep、Lock锁、Condition)
32 0
Python线程锁(Thread Lock)和进程锁(Process Lock)
Python线程锁(Thread Lock)和进程锁(Process Lock)
|
7月前
|
Arthas 测试技术
如何检测由synchronized或Lock引起的线程阻塞问题
如何检测由synchronized或Lock引起的线程阻塞问题
108 0
|
8月前
解决线程安全问题的方式三:Lock锁 ---JDK5.0新增
解决线程安全问题的方式三:Lock锁 ---JDK5.0新增
19 0
|
3月前
|
安全 Java
java多线程之Lock锁原理以及案例实现电影院卖票
java多线程之Lock锁原理以及案例实现电影院卖票
|
3月前
|
Java
Java多线程同步锁、Lock锁和等待唤醒机制及代码演示
Java多线程同步锁、Lock锁和等待唤醒机制及代码演示
|
4月前
|
Java
多线程并发之显示锁Lock与其通信方式Condition源码解读
多线程并发之显示锁Lock与其通信方式Condition源码解读
23 0
|
7月前
|
Java
Java并发编程:使用Lock接口实现线程同步
Java是一种面向对象的编程语言,具有广泛应用于开发各种类型应用程序的能力。并发编程是Java中一个重要的主题,它允许多个任务同时执行,从而提高程序的性能和响应速度。
140 1
JUC学习(三):synchronized和Lock实现线程间通信(包含虚假唤醒的讲解)
JUC学习(三):synchronized和Lock实现线程间通信(包含虚假唤醒的讲解)
JUC学习(三):synchronized和Lock实现线程间通信(包含虚假唤醒的讲解)

热门文章

最新文章