NET中的线程同步问题

NET中的线程同步问题

本文介绍了VB.NET中的线程同步问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,程序员.对不起,我的英语不好
我想问一下线程.我创建了一个使用线程的程序.但是我不明白如何使线程可以彼此同步.如下代码:

Hello programmers. Sorry for my bad English
I would like to ask about thread. I created a program that uses thread. But I do not understand how to make the thread can sync with one another. Code like below:

Imports System.Threading
Imports System.Runtime.CompilerServices

Public Class EnvironmentTest

    Shared Sub Main()
        Dim objA As New Scrambler(0, 0)
        Dim objB As New Scrambler(0, 2)
        Dim objC As New Scrambler(0, 4)

        objA.ThreadWorker.Start()
        objB.ThreadWorker.Start()
        objC.ThreadWorker.Start()
    End Sub
End Class

Public Class Scrambler
    Public rnd As New Random
    Public _Number As Integer
    Public ThreadWorker As New Thread(AddressOf Random)
    Public _Left As Integer
    Public _Top As Integer
    Sub New(ByVal Left As Integer, ByVal Top As Integer)
        Me._Left = Left
        Me._Top = Top
        'ThreadWorker.IsBackground = True
    End Sub
    <MethodImpl(MethodImplOptions.Synchronized)> _
    Public Sub Random()
        While True
            SyncLock Me
                _Number = rnd.Next(0, 9)
                Console.SetCursorPosition(_Left, _Top)
                Console.Write(_Number)
            End SyncLock
        End While
    End Sub
End Class 



Scrambler中的Random 方法上的问题.每次Console.Write 执行时,它就会移到下一列(控制台应该只写到一个列中,因为Console.SetCursorPosition 总是在Console.Write 执行之前执行).

变量rnd.next _Number = (0, 9)
这意味着变量_Number的最大值为0到9或一位数字.但是在上面的代码中,准确地在Console.Write(_Number)上打印的_Number甚至是2位数字.




The problem on the Random method that is in the class Scrambler. Every time The Console.Write execute, it moved to the next column (Console should only write in one column because Console.SetCursorPosition always execute before Console.Write executed).

variable rnd.next _Number = (0, 9)
that means the maximum value for the variable _Number is 0 to 9 or one digit. But in the code above, precisely on Console.Write(_Number), _Number printed is 2 digits even more.


Is there someone who can provide solutions?

推荐答案

Private Shared _Lock As New Object()<br />
<br />
4) In the <code>Random

方法的中,使用: SyncLock _Lock


注意:我不使用VB,因此您可能必须修复语法.

method, use: SyncLock _Lock


Note: I don''t do VB, so you might have to fix the syntax.


Public Class Scrambler
    Public rnd As New Random
    Public _Number As Integer
    Public ThreadWorker As New Thread(AddressOf Random)
    Public _Left As Integer
    Public _Top As Integer
    Public Shared _Lock As New Object ''Add this code
    Sub New(ByVal Left As Integer, ByVal Top As Integer)
        Me._Left = Left
        Me._Top = Top
        ''ThreadWorker.IsBackground = True
    End Sub
    ''<MethodImpl(MethodImplOptions.Synchronized)> _
    Public Sub Random()
        While True
            SyncLock _Lock
                _Number = rnd.Next(0, 9)
                Console.SetCursorPosition(_Left, _Top)
                Console.Write(_Number)
            End SyncLock
        End While
    End Sub
End Class 


这篇关于VB.NET中的线程同步问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:00