Microsoft KB Archive/328811

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

Article ID: 328811

Article Last Modified on 12/30/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.0

-



This article was previously published under Q328811





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:

http://www.microsoft.com/downloads/details.aspx?familyid=6978D761-4A92-4106-A9BC-83E78D4ABC5B&displaylang=en



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section. This problem was first corrected in Microsoft .NET Framework 1.0 Service Pack 3.



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

Visual Basic .NET code Imports System Imports System.Threading Imports System.Runtime.CompilerServices

Namespace 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 = &quot;Thread &quot; + 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 + &quot;: &quot; + _                   (endno - start).ToString + &quot; ms&quot;) If (Thread.CurrentThread.Equals(StopThread)) Then Exit While End If               End While Finally Console.Out.WriteLine(Thread.CurrentThread.Name + &quot; exiting.&quot;) End Try End Sub 'This is the synchronized method. <MethodImpl(MethodImplOptions.Synchronized)> _ Private Shared Sub SynchronizedMethod

End Sub End Class End 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 = &quot;Thread &quot;+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+&quot;: &quot;+(end-start)+&quot; ms&quot;); if (Thread.CurrentThread.Equals(StopThread)) break; }          }            finally {               Console.Out.WriteLine(Thread.CurrentThread.Name+&quot; exiting.&quot;); }      }

//This is the synchronized method. [MethodImpl(MethodImplOptions.Synchronized)] private static void SynchronizedMethod {     }

} } </li> Use one of the following procedures: <ul> If you are using Visual Basic .NET, follow these steps to set the startup object: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click MyApp, and then click Properties. The MyApp Property Pages dialog box appears.</li> In the Startup object box, select Sub Main, and then click OK.</li></ol> </li> If you are using Visual C# .NET, go to step 8.</li></ul> </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li></ol>

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

<div class="references_section">