ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Multithreading with C#
Subject:   Monitor
Date:   2005-01-15 05:17:02
From:   binujohnp
Dear All,

I have a simple multýthreaded program using enter and wait methods. but the result printing is ambýgious. when i used the debug mode it is printing correctlt. why??



Imports System
Imports System.Threading
Module M
Dim a1 As New x1
Dim a2 As New x1
Public i As Integer = 1
Sub main()
Dim t1 As New System.Threading.Thread(AddressOf a1.test)
Dim t2 As New System.Threading.Thread(AddressOf a2.test)


t1.Start()
t2.Start()


End Sub
End Module
Class x1
Public Sub test()
While M.i < 10
Monitor.Enter(Me)
Monitor.Wait(Me, 500)
System.Console.WriteLine("My" & M.i)

M.i = M.i + 1
System.Threading.Thread.Sleep(200)
Monitor.Exit(Me)
End While


End Sub
End Class