FIX: The performance of a static synchronized method is slow in the .NET Framework 1.0

This article was previously published under Q328811
This article has been archived. It is offered "as is" and will no longer be updated.
SYMPTOMS
When multiple threads try to access a static synchronized method at the same time in Microsoft Visual Studio .NET 2002, the performance of the synchronized method slows down.

Note A static synchronized method is a method that uses the MethodImpl(MehtodImplOptions.Synchronized) attribute.
RESOLUTION
To resolve this problem, obtain the latest service pack for the Microsoft .NET Framework 1.0.
To obtain the latest service pack, visit the following Microsoft Web site:
STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section. This problem was first corrected in Microsoft .NET Framework 1.0 Service Pack 3.
MORE INFORMATION

Steps to reproduce the behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project. The New Project dialog box appears.
  3. Under Project Types, click Visual Basic Projects or click Visual C# Projects.
  4. Under Templates, click Console Application.
  5. In the Name box, type MyApp, and then click OK. By default, one of the following files is created:
    • If you are using Microsoft Visual Basic .NET, a file that is named Module1.vb is created.
    • If you are using Microsoft Visual C# .NET, a file that is named Class1.cs is created.
  6. In the file that was created in step 5, replace the existing code with the following code.

    Visual Basic .NET code
    Imports SystemImports System.ThreadingImports System.Runtime.CompilerServicesNamespace StaticSynchronizer   Class Test        Shared StopThread As Thread        Shared Sub Main()            'Create two threads.            Dim threads(1) As Thread            Dim i As Integer            Dim tstart As New ThreadStart(AddressOf (New Test()).run)            For i = 0 To threads.Length - 1                threads(i) = New Thread(tstart)                threads(i).Name = "Thread " + i.ToString                'Start the threads.                threads(i).Start()            Next            Thread.Sleep(5000)            If (threads.Length > 1) Then                StopThread = threads(0)            End If            Thread.Sleep(Timeout.Infinite)        End Sub        Public Sub run()            Try                While (True)                    Dim start As Integer = Environment.TickCount                    Dim i As Integer                    For i = 0 To 5000                        SynchronizedMethod()                    Next                    Dim endno As Long = Environment.TickCount                    Console.Out.WriteLine(Thread.CurrentThread.Name + ": " + _                    (endno - start).ToString + " ms")                    If (Thread.CurrentThread.Equals(StopThread)) Then                        Exit While                    End If                End While            Finally                Console.Out.WriteLine(Thread.CurrentThread.Name + " exiting.")            End Try        End Sub        'This is the synchronized method.        <MethodImpl(MethodImplOptions.Synchronized)> _        Private Shared Sub SynchronizedMethod()        End Sub    End ClassEnd Namespace
    Visual C# .NET code
    using System;using System.Threading;using System.Runtime.CompilerServices;namespace StaticSynchronizer{   class Test   {       static Thread StopThread = null;       static void Main(string[] args)       {          //Create two threads.          Thread[] threads = new Thread[2];		          for (int i = 0; i < threads.Length; i++)           {               threads[i] = new Thread(new ThreadStart(new Test().run));               threads[i].Name = "Thread "+i;               //Start the threads.               threads[i].Start();          }          Thread.Sleep(5000);          if (threads.Length > 1)           {              StopThread = threads[0];          }          Thread.Sleep(Timeout.Infinite);       }       public void run()       {           try            {                while (true)                 {                     int start = Environment.TickCount;                     for (int i = 0; i < 5000; i++)                         SynchronizedMethod();                      long end = Environment.TickCount;                     Console.Out.WriteLine(Thread.CurrentThread.Name+": "+(end-start)+" ms");                     if (Thread.CurrentThread.Equals(StopThread))                          break;			                }           }            finally            {                Console.Out.WriteLine(Thread.CurrentThread.Name+" exiting.");           }       }      //This is the synchronized method.      [MethodImpl(MethodImplOptions.Synchronized)]      private static void SynchronizedMethod()      {			      }   }}
  7. Use one of the following procedures:
    • If you are using Visual Basic .NET, follow these steps to set the startup object:
      1. In Solution Explorer, right-click MyApp, and then click Properties. The MyApp Property Pages dialog box appears.
      2. In the Startup object box, select Sub Main, and then click OK.
    • If you are using Visual C# .NET, go to step 8.
  8. On the Build menu, click Build Solution.
  9. On the Debug menu, click Start.
In the output, notice that the call to the synchronized method takes several seconds to be completed.

For additional information, click the following article number To view the article In the Microsoft Knowledge Base:
824684 Description of the standard terminology that Is used To describe Microsoft software updates
REFERENCES
For more information about MethodImplOptions enumeration, visit the following Microsoft Developer Network (MSDN) Web site:
Properties

Article ID: 328811 - Last Review: 01/17/2015 15:29:58 - Revision: 1.4

Microsoft .NET Framework 1.0

  • kbnosurvey kbarchive kbhotfixserver kbqfe kbthread kbperformance kbqfe kbnetframe100presp3fix kbbug kbfix KB328811
Feedback