| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 服务进程编程 >> 实例分析正文
最新推荐 更多内容
Threading-ReaderWriterSynchronization的实例
Threading-ReaderWriterSynchronization的实例
作者:sdk    来源:microsoft     更新时间:2008-1-9

所属分类 : Threading

ReaderWriterSynchronization的实例

代码如下:

'-----------------------------------------------------------------------
'  This file is part of the Microsoft .NET Framework SDK Code Samples.
'
'  Copyright (C) Microsoft Corporation.  All rights reserved.
'
'This source code is intended only as a supplement to Microsoft
'Development Tools and/or on-line documentation.  See these other
'materials for detailed information regarding Microsoft code samples.
'
'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'PARTICULAR PURPOSE.
'=====================================================================
'  File:      ReaderWriter.vb
'
'  Summary:   Demonstrates use of ReaderWriterLock synchronization
'
'=====================================================================

Option Explicit On
Option Strict On


Imports System
Imports System.Threading


Class Resource
    Private rwl As New ReaderWriterLock()
       
    Public Sub Read(threadNum As Int32)
        rwl.AcquireReaderLock(Timeout.Infinite)
        Try
            Console.WriteLine("Start Resource reading (Thread={0})", threadNum)
            Thread.Sleep(250)
            Console.WriteLine("Stop  Resource reading (Thread={0})", threadNum)
        Finally
            rwl.ReleaseReaderLock()
        End Try
    End Sub 'Read
   
   
    Public Sub Write(threadNum As Int32)
        rwl.AcquireWriterLock(Timeout.Infinite)
        Try
            Console.WriteLine("Start Resource writing (Thread={0})", threadNum)
            Thread.Sleep(750)
            Console.WriteLine("Stop  Resource writing (Thread={0})", threadNum)
        Finally
            rwl.ReleaseWriterLock()
        End Try
    End Sub 'Write
End Class 'Resource

 

Class App
    Private Shared numAsyncOps As Int32 = 20
    Private Shared asyncOpsAreDone As New AutoResetEvent(False)
    Private Shared res As New Resource()
   
   
    Public Shared Sub Main()
        Dim threadNum As Int32
        For threadNum = 0 To 19
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf UpdateResource), threadNum)
        Next threadNum
       
        asyncOpsAreDone.WaitOne()
        Console.WriteLine("All operations have completed.")
    End Sub 'Main
   
   
   
    ' The callback method's signature MUST match that of a System.Threading.TimerCallback
    ' delegate (it takes an Object parameter and returns void)
    Shared Sub UpdateResource(state As Object)
        Dim threadNum As Int32 = CType(state, Int32)
        If threadNum Mod 2 <> 0 Then
            res.Read(threadNum)
        Else
            res.Write(threadNum)
        End If
        If Interlocked.Decrement(numAsyncOps) = 0 Then
            asyncOpsAreDone.Set()
        End If
    End Sub 'UpdateResource
End Class 'App

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    Threading-WaitSynchronization的实例
    Threading-Timers的实例
    Threading-Pools的实例
    Threading-MonitorSynchronization的实例
    Threading-JoinThread的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]