0

我需要实现一个生成唯一组合编号数组的函数吗?许多设备都可以访问此函数(通过使用线程),并且每个设备都应该获得一个唯一的地址 id(sbyte 数组)。

我之前在 C# 中使用了一个函数来生成一个唯一的数字,但我不知道如何在 VB.net 中实现这个新案例。

public class GetUniqueNumber
{
    static private  int id = 0;
    static private final Object lock = new Object();


    public int getId()
    {
        synchronized (lock)
       {
            int temp = id;
            id++;
            return temp;
        }
    }
}
4

3 回答 3

1

You need to use a SyncLock, like this:

Public Class GetUniqueNumber
    Private Shared id As Integer = 0
    Private Shared lock = New Object()

    Public Function getId() As Integer
        SyncLock lock
            Dim temp As Integer = id
            id += 1
            Return temp
        End SyncLock
    End Function
End Class
于 2013-08-21T14:41:30.533 回答
1

作为整数

Public Class GetUniqueNumber
    Private Shared id As Integer = 0
    Private Shared lock As New Object
    Public ReadOnly Property getID() As Integer
        Get
            Dim temp As Integer
            SyncLock lock
                temp = id
                id += 1
            End SyncLock
            Return temp
        End Get
    End Property
End Class

作为字节数组

Public Class GetUniqueNumber
    Private Shared id As Integer = 0
    Private Shared lock As New Object

    Public ReadOnly Property getID() As Byte()
        Get
            Dim temp As Byte()
            SyncLock lock
                temp = BitConverter.GetBytes(id)
                id += 1
            End SyncLock
            Return temp
        End Get
    End Property
End Class

作为来自 int16 的字节数组

Public Class GetUniqueNumber
    Private Shared id As Int16 = 0
    Private Shared lock As New Object

    Public ReadOnly Property getID() As Byte()
        Get
            Dim temp As Byte()
            SyncLock lock
                temp = BitConverter.GetBytes(id)
                id += 1S
            End SyncLock
            Return temp
        End Get
    End Property
End Class

使用大整数

Public Class GetUniqueNumber
    Private Shared id As BigInteger = 0
    Private Shared lock As New Object

    Public ReadOnly Property getID() As Byte()
        Get
            Dim temp As Byte()
            SyncLock lock
                temp = id.ToByteArray
                id += 1
            End SyncLock
            If temp.Length <> 16 Then
                Array.Resize(temp, 16)
            End If
            Return temp
        End Get
    End Property
End Class

或者

Public Class GetUniqueNumber
    Private Shared idLS As Long = 0L
    Private Shared idMS As Long = 0L
    Private Shared lock As New Object

    Public ReadOnly Property getID() As Byte()
        Get
            Dim tempLS() As Byte
            Dim tempMS() As Byte
            Dim rv(15) As Byte
            SyncLock lock
                tempLS = BitConverter.GetBytes(idLS)
                tempMS = BitConverter.GetBytes(idMS)
                If idLS = Long.MaxValue Then
                    idMS += 1L
                    idLS = 0L
                Else
                    idLS += 1L
                End If
                Array.Reverse(tempLS)
                Array.Reverse(tempMS)
                Array.Copy(tempLS, 0, rv, 8, tempLS.Length)
                Array.Copy(tempMS, 0, rv, 0, tempMS.Length)
            End SyncLock
            Return rv
        End Get
    End Property
End Class
于 2013-08-21T14:42:53.237 回答
0

像这样 ?

' Define other methods and classes here
public class GetUniqueNumber
    shared id as integer=0
    shared readonly lock =new Object()
    public function getId() as integer

        SyncLock lock
            dim temp =id
            id=id+1
            return temp
        end synclock
    end function
    end class
于 2013-08-21T14:39:03.943 回答