Create multiple threads of execution : Thread Creation « Thread « C# / CSharp Tutorial

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. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » Thread » Thread Creation 
20. 1. 5. Create multiple threads of execution
using System; 
using System.Threading; 
 
class MyThread 
  public int count; 
  public Thread thrd; 
 
  public MyThread(string name) { 
    count = 0
    thrd = new Thread(this.run)
    thrd.Name = name; 
    thrd.Start()
  
 
  void run() { 
    Console.WriteLine(thrd.Name + " starting.")
 
    do 
      Thread.Sleep(500)
      Console.WriteLine("In " + thrd.Name + 
                        ", count is " + count)
      count++; 
    while(count < 10)
 
    Console.WriteLine(thrd.Name + " terminating.")
  

 
class MainClass 
  public static void Main() { 
    Console.WriteLine("Main thread starting.")
 
    MyThread mt1 = new MyThread("Child #1")
    MyThread mt2 = new MyThread("Child #2")
    MyThread mt3 = new MyThread("Child #3")
 
    Thread.Sleep(10000)
 
    Console.WriteLine("Main thread ending.")
  
}
Main thread starting.
Child #1 starting.
Child #2 starting.
Child #3 starting.
In Child #1, count is 0
In Child #2, count is 0
In Child #3, count is 0
In Child #1, count is 1
In Child #2, count is 1
In Child #3, count is 1
In Child #1, count is 2
In Child #2, count is 2
In Child #3, count is 2
In Child #1, count is 3
In Child #2, count is 3
In Child #3, count is 3
In Child #1, count is 4
In Child #2, count is 4
In Child #3, count is 4
In Child #1, count is 5
In Child #2, count is 5
In Child #3, count is 5
In Child #2, count is 6
In Child #1, count is 6
In Child #3, count is 6
In Child #2, count is 7
In Child #1, count is 7
In Child #3, count is 7
In Child #2, count is 8
In Child #1, count is 8
In Child #3, count is 8
In Child #2, count is 9
Child #2 terminating.
In Child #1, count is 9
Child #1 terminating.
In Child #3, count is 9
Child #3 terminating.
^CTerminate batch job (Y/N)? n
20. 1. Thread Creation
20. 1. 1. Thread method with parameter
20. 1. 2. Thread method with no parameter
20. 1. 3. The creation of threads
20. 1. 4. Create a thread of execution
20. 1. 5. Create multiple threads of execution
20. 1. 6. Passing an argument to the thread method.
20. 1. 7. Use anonymous delegate as the worker method to create Thread
20. 1. 8. Adding with Thread objects
20. 1. 9. Communicating Data to a Thread
20. 1. 10. Primary Thread statistcs
ww__w.__j___av_a___2__s.__c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.