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

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 服务进程编程 >> 实例分析正文
最新推荐 更多内容
[图文]线程缓冲的实例
线程缓冲的实例
作者:佚名    来源:乐博网收集     更新时间:2007-11-29
Circular Buffer Demo

Imports System
Imports System.Threading

Public Class MainClass

   Shared Sub Main()
      ' create shared object
      Dim sharedLocation As New CHoldIntegerSynchronized()

      sharedLocation.CreateStateOutput()

      ' Random object used by each thread
      Dim randomObject As New Random()

      ' create CProducer and CConsumer objects
      Dim producer As New CProducer(sharedLocation, randomObject)

      Dim consumer As New CConsumer(sharedLocation, randomObject)

      ' create threads
      Dim producerThread As New Thread(AddressOf producer.Produce)

      Dim consumerThread As New Thread(AddressOf consumer.Consume)

      ' name threads
      producerThread.Name = "Producer"
      consumerThread.Name = "Consumer"

      ' start threads
      producerThread.Start()
      consumerThread.Start()

   End Sub ' Main

End Class

Public Class CConsumer
   Private sharedLocation As CHoldIntegerSynchronized
   Private randomSleepTime As Random

   ' constructor
   Public Sub New(ByVal sharedObject As CHoldIntegerSynchronized, _
      ByVal randomObject As Random)

      sharedLocation = sharedObject
      randomSleepTime = randomObject
   End Sub

   Public Sub Consume()
      Dim count, sum As Integer

      For count = To 10
         Thread.Sleep(randomSleepTime.Next(13000))
         sum += sharedLocation.Buffer
      Next

      Console.WriteLine"Total " & _
         Thread.CurrentThread.Name & " consumed: " & sum & vbCrLf & _
         Thread.CurrentThread.Name & " terminated." )

   End Sub

End Class

Public Class CHoldIntegerSynchronized

   Private mBuffer As Integer() {-1, -1, -1}

   Private occupiedBufferCount As Integer

   Private readlocation, writeLocation As Integer

   Public Sub New()
   End Sub ' New

   Property Buffer() As Integer
      Get
         SyncLock (Me)
            If occupiedBufferCount = Then
               Console.WriteLine"All buffers empty. " & _
                  Thread.CurrentThread.Name & " waits." )

               Monitor.Wait(Me)
            End If

            Dim readValue As Integer = mBuffer(readlocation)

            Console.WriteLineThread.CurrentThread.Name & " reads " & _
               mBuffer(readlocation) )

            occupiedBufferCount -= 1

            readlocation = (readlocation + 1Mod mBuffer.Length

            CreateStateOutput() 

            Monitor.Pulse(Me)

            Return readValue
         End SyncLock

      End Get

      Set(ByVal Value As Integer)
         SyncLock (Me)
            If occupiedBufferCount = mBuffer.Length Then
               Console.WriteLine"All buffers full. " & _
                  Thread.CurrentThread.Name & " waits." )

               Monitor.Wait(Me)
            End If

            mBuffer(writeLocation= Value

            Console.WriteLineThread.CurrentThread.Name & " writes " & _
               mBuffer(writeLocation) )

            occupiedBufferCount += 1

            writeLocation = (writeLocation + 1Mod _
               mBuffer.Length

            CreateStateOutput() 
            Monitor.Pulse(Me)
         End SyncLock

      End Set

   End Property ' Buffer

   Public Sub CreateStateOutput()

      Dim As Integer
      Console.WriteLine"(buffers occupied: " & _
         occupiedBufferCount & ")" & vbCrLf & "buffers: " )

      For i = To mBuffer.GetUpperBound(0)
         Console.Write" " & mBuffer(i"  " )
      Next

      For i = To mBuffer.GetUpperBound(0)
         Console.Write"---- " )
      Next


      For i = To mBuffer.GetUpperBound(0)

         If (i = writeLocation AndAlso _
            writeLocation = readlocationThen

            Console.Write" WR  " )
         ElseIf i = writeLocation Then
            Console.Write" W   " )
         ElseIf i = readlocation Then
            Console.Write"  R  " )
         Else
            Console.Write"     " )
         End If

      Next
   End Sub

End Class

Public Class CProducer
   Private sharedLocation As CHoldIntegerSynchronized
   Private randomSleepTime As Random

   Public Sub New(ByVal sharedObject As CHoldIntegerSynchronized, _
      ByVal randomObject As Random)

      sharedLocation = sharedObject
      randomSleepTime = randomObject
   End Sub ' New

   Public Sub Produce()
      Dim count As Integer

      For count = 11 To 20
         Thread.Sleep(randomSleepTime.Next(13000))
         sharedLocation.Buffer = count
      Next

      Console.WriteLineThread.CurrentThread.Name & _
         " done producing. " & vbCrLf & _
         Thread.CurrentThread.Name & " terminated." )
   End Sub

End Class

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    多个线程同时开启的简单实例

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