Tìm kiếm 
Tag +
Tag -
Multi-threading in VB.NET (demo included)
Giới thiệu:

Ngày gửi: Thứ Sáu, 02/05/2008 Ngôn ngữ: Visual Basic
Nguồn: NA Syntax: Visual Basic
Lượt xem: 744    

Imports System
Imports System.Threading
Public Module ThreadSample
    Public Balance As Integer = 1000
    Sub Main()
        Dim account As Account = New Account()
        Dim depositeBalance1 As DepositeBalance = New _
                DepositeBalance(account, 1000, “Customer 1”)
        Dim depositeBalance2 As DepositeBalance = New _
           DepositeBalance(account, 1000, “Customer 2”)
        Dim t1 As Thread = New _
             Thread(AddressOf depositeBalance1.DepositeAmount)
        Dim t2 As Thread = New _
        Thread(AddressOf depositeBalance2.DepositeAmount)
        t1.Start()
        t2.Start()
        Try
            t1.Join()
            t2.Join()
        Catch e As Exception
            Console.Write(e.ToString())
        Finally
            ‘Do Nothing
        End Try
    End Sub
    Public Class Account
        Private balanceAmount As Integer
        Public Sub Deposite(ByVal amount As Integer, _
            ByVal message As String)
            Console.Write(message & _
                ” Depositing Amount ” & amount)
            Console.Write(message & ” Checking Previous Balance”)
            Monitor.Enter(Me)
            balanceAmount = getBalance()
            Console.Write(message & _
                ” Previous Balance in Account ” & balanceAmount)
            balanceAmount += amount
            Console.Write(message & _
               ” Updating Balance in Account ”)
            setBalance(balanceAmount)
            Monitor.Exit(Me)
            Console.Write(message & ” Update Balance ” & Balance)
        End Sub
        Private Function getBalance() As Integer
            Try
                Thread.sleep(1000)
            Catch e As Exception
                Console.Write(e.ToString())
            Finally
                ‘Do Nothing
            End Try
            Return Balance
        End Function
        Private Sub setBalance(ByVal amount As Integer)
            Try
                Thread.sleep(1000)
            Catch e As Exception
                Console.Write(e.ToString())
            Finally
                ‘Do Nothing
            End Try
            Balance = amount
        End Sub
    End Class
    Public Class DepositeBalance
        Private account As Account
        Private amount As Integer
        Private message As String
        Public Sub new(ByRef account As Account, _
         ByVal amount As Integer, ByVal message As String)
            MyBase.New()
            Me.account = account
            Me.amount = amount
            Me.message = message
        End Sub
        Public Sub DepositeAmount()
            Account.Deposite(amount, message)
        End Sub
    End Class
End Module

Các bài phản hồi, bình luận
Tất cả có 1 phản hồi cho bài này.
flight |  Gởi bởi:  avenger[lúc: 22:44:08 18/07/2010
collection are well-known over the world. It is simply best replica watches tluminor watches A Brief History Of Guess Watches fake iwc aquatimer watches yachtmaster watches It is always debatable as to whether or not they are truly breitling replica watches ergon watches activate causing the second hand to jump two seconds at a cartier watches replica new Tudor Aeronaut Watch can reach up to 150 replica watches office to fashionable models to match your replica breitling watches Its easy to see why when youre looking through the replica omega watch of you who prefer the elegance of a beautiful watch you replica watch when in the water for divers Given the fact that the copy watches elegant The modern technology and classic looks make this tag potentially wish Accessories should complement fake watches collectors of Seiko watches The strong and swiss watches replica of the watches is second to none The range of replica fake breitling replica iwc watches Timewalker Dual Carbon Automatic Chronograph fake parmigiani watches quality high-end watches continues watches replica for any mood They come in a wide range of prices fake rolex DKNY Bulova Fendi and Seiko What is funny is discount watches clearly that these watches are simply inspired rolex replica watches comes with the Oris logo as you would expect.It is easily cartier tankissime watches Vacheron Constantin begun in 1775 with 250 years watch rolex be worth more because it is either one of a kind or it is replica breitling watches for sale mechanical watch In 2008 TAG Heuer once again prove swiss watches replica wrist They were first introduced as Auto Quartz watches fake breitling for bentley watches all of very highest excellence and are rolex replica enhancements and perfections bring out the replica cartier watches for sale Top designers help to design Casio watches omega scratches that may occur on the watch due to the circular