VB.NET中的线程同步问题? [英] Thread Synchronized Problem in VB.NET?

查看:103
本文介绍了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?

推荐答案

您确实有比赛条件.这是因为您锁定了我,所以每个对象都锁定了自己,而不是某些共享对象.所以:

1)永远不要使用MethodImplOptions.Synchronized

2)永远不要使用 SyncLock Me -

3)将共享对象添加到Scrambler:
You do indeed have a race condition. This is because you are locking Me, so each object is locking itself, not some shared object. So:

1) Don''t use MethodImplOptions.Synchronized - ever!

2) Don''t use SyncLock Me - ever!

3) Add a shared object to Scrambler:
Private Shared _Lock As New Object()<br />
<br />
4) In the <code>Random

方法的中,使用:

method, use: SyncLock _Lock


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


感谢尼古拉斯,您给了我解决方案

这是Class Scrambler的正确代码:
Thanks Nicholas, you gave me the solution

Here is the right code for Class Scrambler :
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中的线程同步问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆