ThreadStatic field : ThreadStatic « Thread « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Thread » ThreadStatic 
23.10.1.ThreadStatic field
Imports System.Threading

Public Class SharedByThread
   <ThreadStatic> Private Shared Count As Integer

   Public Shared Sub Main()
   
      Dim secondThread As New thread(AddressOf Thread2Proc)
      secondThread.Start()

      count = 100

      For ctr As Integer = to 10
         Console.WriteLine("The value of count in the main thread is {0}.", count)
         IncrementCounter(200)
      Next
   End Sub

   Public Shared Sub Thread2Proc()
      count = 0

      For ctr As Integer = to 10
         Console.WriteLine("The value of count in the second thread is {0}.", count)
         IncrementCounter(250)
      Next
   End Sub

   Public Shared Sub IncrementCounter(delay As Integer)
      count +=1
      Thread.Sleep(delay)
   End Sub
End Class
The value of count in the second thread is 0.
The value of count in the main thread is 100.
The value of count in the main thread is 101.
The value of count in the second thread is 1.
The value of count in the main thread is 102.
The value of count in the second thread is 2.
The value of count in the main thread is 103.
The value of count in the second thread is 3.
The value of count in the main thread is 104.
The value of count in the second thread is 4.
The value of count in the main thread is 105.
The value of count in the main thread is 106.
The value of count in the second thread is 5.
The value of count in the main thread is 107.
The value of count in the second thread is 6.
The value of count in the main thread is 108.
The value of count in the second thread is 7.
The value of count in the main thread is 109.
The value of count in the second thread is 8.
The value of count in the main thread is 110.
The value of count in the second thread is 9.
The value of count in the second thread is 10.
23.10.ThreadStatic
23.10.1.ThreadStatic field
23.10.2.ThreadStatic
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.