Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,50 @@
|
|||
Imports System.Threading
|
||||
Module Module1
|
||||
Public rnd As New Random
|
||||
|
||||
Sub Main()
|
||||
'Aristotle, Kant, Spinoza, Marx, and Russel
|
||||
Dim f1 As New Fork(1)
|
||||
Dim f2 As New Fork(2)
|
||||
Dim f3 As New Fork(3)
|
||||
Dim f4 As New Fork(4)
|
||||
Dim f5 As New Fork(5)
|
||||
|
||||
Console.WriteLine("1: Deadlock")
|
||||
Console.WriteLine("2: Live lock")
|
||||
Console.WriteLine("3: Working")
|
||||
Select Console.ReadLine
|
||||
Case "1"
|
||||
Using _
|
||||
Aristotle As New SelfishPhilosopher("Aristotle", f1, f2), _
|
||||
Kant As New SelfishPhilosopher("Kant", f2, f3), _
|
||||
Spinoza As New SelfishPhilosopher("Spinoza", f3, f4), _
|
||||
Marx As New SelfishPhilosopher("Marx", f4, f5), _
|
||||
Russel As New SelfishPhilosopher("Russel", f5, f1)
|
||||
|
||||
Console.ReadLine()
|
||||
End Using
|
||||
Case "2"
|
||||
Using _
|
||||
Aristotle As New SelflessPhilosopher("Aristotle", f1, f2), _
|
||||
Kant As New SelflessPhilosopher("Kant", f2, f3), _
|
||||
Spinoza As New SelflessPhilosopher("Spinoza", f3, f4), _
|
||||
Marx As New SelflessPhilosopher("Marx", f4, f5), _
|
||||
Russel As New SelflessPhilosopher("Russel", f5, f1)
|
||||
|
||||
Console.ReadLine()
|
||||
End Using
|
||||
Case "3"
|
||||
Using _
|
||||
Aristotle As New WisePhilosopher("Aristotle", f1, f2), _
|
||||
Kant As New WisePhilosopher("Kant", f2, f3), _
|
||||
Spinoza As New WisePhilosopher("Spinoza", f3, f4), _
|
||||
Marx As New WisePhilosopher("Marx", f4, f5), _
|
||||
Russel As New WisePhilosopher("Russel", f5, f1)
|
||||
|
||||
Console.ReadLine()
|
||||
End Using
|
||||
End Select
|
||||
End Sub
|
||||
|
||||
End Module
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
Class Fork
|
||||
Private ReadOnly m_Number As Integer
|
||||
Public Sub New(ByVal number As Integer)
|
||||
m_Number = number
|
||||
End Sub
|
||||
Public ReadOnly Property Number() As Integer
|
||||
Get
|
||||
Return m_Number
|
||||
End Get
|
||||
End Property
|
||||
End Class
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
MustInherit Class PhilosopherBase
|
||||
Implements IDisposable
|
||||
|
||||
Protected m_Disposed As Boolean
|
||||
Protected ReadOnly m_Left As Fork
|
||||
Protected ReadOnly m_Right As Fork
|
||||
Protected ReadOnly m_Name As String
|
||||
Public Sub New(ByVal name As String, ByVal right As Fork, ByVal left As Fork)
|
||||
m_Name = name
|
||||
m_Right = right
|
||||
m_Left = left
|
||||
Dim t As New Thread(AddressOf MainLoop)
|
||||
t.IsBackground = True
|
||||
t.Start()
|
||||
End Sub
|
||||
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
|
||||
m_Disposed = True
|
||||
End Sub
|
||||
|
||||
Public Sub Dispose() Implements IDisposable.Dispose
|
||||
Dispose(True)
|
||||
GC.SuppressFinalize(Me)
|
||||
End Sub
|
||||
Public ReadOnly Property Name() As String
|
||||
Get
|
||||
Return m_Name
|
||||
End Get
|
||||
End Property
|
||||
|
||||
Public MustOverride Sub MainLoop()
|
||||
End Class
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
Class SelfishPhilosopher
|
||||
Inherits PhilosopherBase
|
||||
Public Sub New(ByVal name As String, ByVal right As Fork, ByVal left As Fork)
|
||||
MyBase.New(name, right, left)
|
||||
End Sub
|
||||
|
||||
Public Overrides Sub MainLoop()
|
||||
Do
|
||||
Console.WriteLine(Name & " sat down")
|
||||
SyncLock m_Left
|
||||
Console.WriteLine(Name & " picked up fork " & m_Left.Number)
|
||||
|
||||
SyncLock m_Right
|
||||
Console.WriteLine(Name & " picked up fork " & m_Right.Number)
|
||||
|
||||
Console.WriteLine(Name & " ate!!!!")
|
||||
|
||||
Console.WriteLine(Name & " put down fork " & m_Right.Number)
|
||||
End SyncLock
|
||||
|
||||
|
||||
Console.WriteLine(Name & " put down fork " & m_Left.Number)
|
||||
End SyncLock
|
||||
|
||||
Console.WriteLine(Name & " stood up")
|
||||
|
||||
Thread.Sleep(rnd.Next(0, 10000))
|
||||
Loop Until m_Disposed
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
Class SelflessPhilosopher
|
||||
Inherits PhilosopherBase
|
||||
|
||||
Public Sub New(ByVal name As String, ByVal right As Fork, ByVal left As Fork)
|
||||
MyBase.New(name, right, left)
|
||||
End Sub
|
||||
|
||||
Public Overrides Sub MainLoop()
|
||||
Do
|
||||
Console.WriteLine(Name & " sat down")
|
||||
Dim needDelay = False
|
||||
TryAgain:
|
||||
If needDelay Then Thread.Sleep(rnd.Next(0, 10000))
|
||||
Try
|
||||
Monitor.Enter(m_Left)
|
||||
Console.WriteLine(Name & " picked up fork " & m_Left.Number)
|
||||
|
||||
If Monitor.TryEnter(m_Right) Then
|
||||
Console.WriteLine(Name & " picked up fork " & m_Right.Number)
|
||||
|
||||
Console.WriteLine(Name & " ate!!!!!!")
|
||||
|
||||
Console.WriteLine(Name & " put down fork " & m_Right.Number)
|
||||
Monitor.Exit(m_Right)
|
||||
Else
|
||||
Console.WriteLine(Name & " is going to wait")
|
||||
needDelay = True
|
||||
GoTo TryAgain
|
||||
End If
|
||||
Finally
|
||||
Console.WriteLine(Name & " put down fork " & m_Left.Number)
|
||||
End Try
|
||||
|
||||
Console.WriteLine(Name & " stood up")
|
||||
|
||||
Thread.Sleep(rnd.Next(0, 10000))
|
||||
|
||||
Loop Until m_Disposed
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
Class WisePhilosopher
|
||||
Inherits PhilosopherBase
|
||||
Public Sub New(ByVal name As String, ByVal right As Fork, ByVal left As Fork)
|
||||
MyBase.New(name, right, left)
|
||||
End Sub
|
||||
|
||||
Public Overrides Sub MainLoop()
|
||||
Do
|
||||
Console.WriteLine(Name & " sat down")
|
||||
|
||||
Dim first As Fork, second As Fork
|
||||
If m_Left.Number > m_Right.Number Then
|
||||
first = m_Left
|
||||
second = m_Right
|
||||
Else
|
||||
first = m_Right
|
||||
second = m_Left
|
||||
End If
|
||||
|
||||
SyncLock first
|
||||
Console.WriteLine(Name & " picked up fork " & m_Left.Number)
|
||||
|
||||
SyncLock second
|
||||
Console.WriteLine(Name & " picked up fork " & m_Right.Number)
|
||||
|
||||
Console.WriteLine(Name & " ate!!!!")
|
||||
|
||||
Console.WriteLine(Name & " put down fork " & m_Right.Number)
|
||||
End SyncLock
|
||||
|
||||
|
||||
Console.WriteLine(Name & " put down fork " & m_Left.Number)
|
||||
End SyncLock
|
||||
|
||||
Console.WriteLine(Name & " stood up")
|
||||
|
||||
Thread.Sleep(rnd.Next(0, 10000))
|
||||
Loop Until m_Disposed
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
Loading…
Add table
Add a link
Reference in a new issue