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

稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor,第1张

概述[索引页] [源码下载] 稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor, ThreadStaticAttribute 作者:webabcd 介绍 Silverlight 2.0 使用Lock, Interlocked, EventWaitHandle, Monitor来实现线程同步     Lock [索引页]
[源码下载]


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

作者:webabcd


介绍
Silverlight 2.0 使用Lock,Monitor来实现线程同步
    Lock - 确保代码块完成运行,而不会被其他线程中断
    Interlocked - 为多个线程共享的变量提供原子级的 *** 作
    EventWaitHandle - 通知其他线程是否可入的类
    Monitor - 提供同步访问对象的机制
    ThreadStaticAttribute - 所指定的静态变量对每个线程都是唯一的


在线DEMO
http://www.voidcn.com/article/p-ounmxjds-tq.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

        {

                 // 需要被 lock 的静态变量

                 private static Readonly object objLock = new object();


                 private static int i;


                 public Lock()

                {

                        InitializeComponent();


                        i = 0;


                         for ( int x = 0; x < 100; x++)

                        {

                                 // 开 100 个线程去 *** 作静态变量 i

                                System.Threading.Thread thread = new System.Threading.Thread( new System.Threading.ThreadStart(DoWork));

                                thread.Start();

                        }


                        System.Threading.Thread.Sleep(3000);

                         // 3 秒后 100 个线程都应该执行完毕了,取得 i 的结果

                         // 做了并发处理的结果为 100 ,去掉 lock 可得到不做并发处理的结果

                        txtMsg.Text = i.ToString();

                }


                 private voID DoWork()

                {

                         try

                        {

                                 // lock() - 确保代码块完成运行,而不会被其他线程中断。其参数必须为一个引用类型的对象

                                 lock (objLock)

                                {

                                         int j = i + 1;


                                         // 模拟多线程并发 *** 作静态变量 i 的情况

                                        System.Threading.Thread.Sleep(10);


                                        i = j;

                                }

                        }

                         finally

                        {

                                 // code

                        }

                }

        }

}     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 = 0;


                         for ( int x = 0; x < 100; x++)

                        {

                                 // 开 100 个线程去 *** 作静态变量 i

                                System.Threading.Thread thread = new System.Threading.Thread( new System.Threading.ThreadStart(DoWork));

                                thread.Start();

                        }


                        System.Threading.Thread.Sleep(1000);

                         // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果

                        txtMsg.Text = i.ToString();

                }


                 private voID DoWork()

                {

                         try

                        {

                                 // Interlocked - 为多个线程共享的变量提供原子级的 *** 作(避免并发问题)


                                 // i 加 1

                                System.Threading.Interlocked.Increment( ref i);


                                 // i 减 1

                                System.Threading.Interlocked.Decrement( ref i);


                                 // i 加 1

                                System.Threading.Interlocked.Add( ref i,1);


                                 // 如果 i 等于 100 ,则将 i 赋值为 101

                                System.Threading.Interlocked.CompareExchange( ref i,101,100);    


                                 // 将 i 赋值为 1000

                                 // System.Threading.Interlocked.Exchange(ref i,1000);

                        }

                         finally

                        {

                                 // code

                        }

                }

        }

}     未完待续>>     OK
[源码下载]
总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor全部内容,希望文章能够帮你解决稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1052285.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存