Thread Static field : Static Thread Field « Thread « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Thread » Static Thread Field 
20.27.2.Thread Static field
using System;
using System.Threading;

public class Task {
 
  [ThreadStatic
  static int id = 10;

  public string  threadName;
  
  public void Runstring ThreadName ) {
    this.threadName = ThreadName;
    Thread T = new Threadnew ThreadStartprocess ) );
    T.Start( );
  }

  public void process( ) {
    Console.WriteLine"Thread {0} is running", threadName );

    for(int i = 0; i < 10; i++ )
      Console.WriteLine("Thread {0} : id = {1}"this.threadName, id++ );
  }
}


public class MainClass {

  public static void Main( ) {
    Task t1 = new Task( );
    Task t2 = new Task( );

    t1.Run"Worker 1" );
    t2.Run"Worker 2" );
    
    Task t3 = new Task( );
    t3.threadName = "Main Thread";
    t3.process( );
  }
}
Thread Worker 1 is running
Thread Worker 1 : id = 0
Thread Worker 1 : id = 1
Thread Worker 1 : id = 2
Thread Worker 1 : id = 3
Thread Worker 1 : id = 4
Thread Worker 1 : id = 5
Thread Worker 1 : id = 6
Thread Worker 1 : id = 7
Thread Worker 1 : id = 8
Thread Worker 1 : id = 9
Thread Worker 2 is running
Thread Worker 2 : id = 0
Thread Worker 2 : id = 1
Thread Worker 2 : id = 2
Thread Worker 2 : id = 3
Thread Worker 2 : id = 4
Thread Worker 2 : id = 5
Thread Worker 2 : id = 6
Thread Worker 2 : id = 7
Thread Worker 2 : id = 8
Thread Worker 2 : id = 9
Thread Main Thread is running
Thread Main Thread : id = 10
Thread Main Thread : id = 11
Thread Main Thread : id = 12
Thread Main Thread : id = 13
Thread Main Thread : id = 14
Thread Main Thread : id = 15
Thread Main Thread : id = 16
Thread Main Thread : id = 17
Thread Main Thread : id = 18
Thread Main Thread : id = 19
20.27.Static Thread Field
20.27.1.Static Thread field
20.27.2.Thread Static field
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.