⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coroutine.vb

📁 this is simple code for guiding students for working in concurrency activities
💻 VB
字号:
Imports System.Threading
Public Class coroutine
    Dim sc As New Semaphore(0, 3)
    Dim rc As New Semaphore(0, 3)
    Dim pc As New Semaphore(0, 3)
    Dim rs, sp As String
    Dim inbuf As String() = New String(99) {}
    Dim outbuf As String() = New String(99) {}
    Sub read()
        Dim i As Integer = 0
        Dim k As Integer = 0
        For k = 0 To 10
            inbuf(k) = Console.Read()
        Next

        While (True)
            For i = 0 To 79                       
                rs = inbuf(i)
                squash()
                sc.Release()
            Next
            rs = " "
            squash()
            sc.Release()
            rc.WaitOne()
        End While

    End Sub
    Sub print()
        Dim j As Integer

        While (True)
            For j = 0 To 20
                pc.WaitOne()
                outbuf(j) = sp
                sc.Release()
            Next
        End While
        For j = 0 To 20
            Console.Write(outbuf(j))
        Next
    End Sub
    Sub squash()
        While (True)
            sc.WaitOne()
            If (rs <> "*") Then
                sp = rs
                print()
            Else
                rc.Release()
                sc.WaitOne()
                If (rs = "*") Then
                    sp = "!"
                    pc.Release()
                    sc.WaitOne()
                Else
                    sp = "*"
                    pc.Release()
                    sp = rs
                    sc.WaitOne()

                End If

            End If
            rc.Release()

        End While      
    End Sub

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -