Posted by: Sourav | October 15, 2016

Resolving a deadlock situation in a multi threaded situation by defining a specific order to acquire locks,Vb teacher Sourav,Kolkata 09748184075


Imports System.Threading
Module Module1

Public Sub Main()
Console.WriteLine(“Main Started”)
Dim accountA As New Account(101, 5000)
Dim accountB As New Account(102, 3000)

Dim accountManagerA As New AccountManager(accountA, accountB, 1000)
Dim T1 As New Thread(AddressOf accountManagerA.Transfer)
T1.Name = “T1”

Dim accountManagerB As New AccountManager(accountB, accountA, 2000)
Dim T2 As New Thread(AddressOf accountManagerB.Transfer)
T2.Name = “T2”

T1.Start()
T2.Start()

T1.Join()
T2.Join()
Console.WriteLine(“Main Completed”)
Console.ReadLine()

End Sub

Public Class Account
Private _balance As Double
Private _id As Integer

Public Sub New(ByVal id As Integer, ByVal balance As Double)
Me._id = id
Me._balance = balance
End Sub

Public ReadOnly Property ID() As Integer
Get
Return _id
End Get
End Property

Public Sub Withdraw(ByVal amount As Double)
_balance -= amount
End Sub

Public Sub Deposit(ByVal amount As Double)
_balance += amount
End Sub
End Class

Public Class AccountManager
Private _fromAccount As Account
Private _toAccount As Account
Private _amountToTransfer As Double

Public Sub New(ByVal fromAccount As Account, ByVal toAccount As Account, ByVal amountToTransfer As Double)
Me._fromAccount = fromAccount
Me._toAccount = toAccount
Me._amountToTransfer = amountToTransfer
End Sub

Public Sub Transfer()
Dim _lock1 As Object, _lock2 As Object

If _fromAccount.ID < _toAccount.ID Then
_lock1 = _fromAccount
_lock2 = _toAccount
Else
_lock1 = _toAccount
_lock2 = _fromAccount
End If

Console.WriteLine(Thread.CurrentThread.Name + ” trying to acquire lock on ” + DirectCast(_lock1, Account).ID.ToString())

SyncLock _lock1
Console.WriteLine(Thread.CurrentThread.Name + ” acquired lock on ” + DirectCast(_lock1, Account).ID.ToString())

Console.WriteLine(Thread.CurrentThread.Name + ” suspended for 1 second”)

Thread.Sleep(1000)
Console.WriteLine(Thread.CurrentThread.Name + ” back in action and trying to acquire lock on ” + DirectCast(_lock2, Account).ID.ToString())

SyncLock _lock2
Console.WriteLine(Thread.CurrentThread.Name + ” acquired lock on ” + DirectCast(_lock2, Account).ID.ToString())

_fromAccount.Withdraw(_amountToTransfer)
_toAccount.Deposit(_amountToTransfer)

Console.WriteLine(Thread.CurrentThread.Name + ” Transfered ” + _amountToTransfer.ToString() + ” from ” + _fromAccount.ID.ToString() + ” to ” + _toAccount.ID.ToString())
End SyncLock
End SyncLock
End Sub
End Class

‘=======================================================
‘Service provided by Telerik (www.telerik.com)
‘Conversion powered by NRefactory.
‘Twitter: @telerik
‘Facebook: facebook.com/telerik
‘=======================================================

End Module

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: