coroutine.vb

来自「this is simple code for guiding students」· VB 代码 · 共 71 行

VB
71
字号
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 + =
减小字号Ctrl + -
显示快捷键?