Logo
Section 1 / 21

使用线程

本课程讨论了创建和运行线程的各种方法。

导入

使用线程

本课程讨论了创建和运行线程的各种方法。

1using System; 2using System.Threading; 3 4class Demonstration 5{ 6 static void Main() 7 { 8 new UsingThreadStart().runTest(); 9 } 10} 11 12public class UsingThreadStart 13{ 14 static void sayHi() 15 { 16 Console.WriteLine("Hi from static method"); 17 } 18 19 void sayHello() 20 { 21 Console.WriteLine("Hello from instance method"); 22 } 23 24 public void runTest() 25 { 26 ThreadStart threadStart = new ThreadStart(sayHi); 27 Thread thread = new Thread(threadStart); 28 thread.Start(); 29 thread.Join(); 30 31 threadStart = new ThreadStart(new UsingThreadStart().sayHello); 32 thread = new Thread(threadStart); 33 thread.Start(); 34 thread.Join(); 35 } 36}
1using System; 2using System.Threading; 3 4class Demonstration 5{ 6 static void Main() 7 { 8 new UsingThreadStart().runTest(); 9 } 10} 11 12public class UsingThreadStart 13{ 14 static void sayHi(Object obj) 15 { 16 String name = (String)obj; 17 Console.WriteLine("Hi " + name + " from static method"); 18 } 19 20 void sayHello(Object obj) 21 { 22 String name = (String)obj; 23 Console.WriteLine("Hello " + name + " from instance method"); 24 } 25 26 public void runTest() 27 { 28 ParameterizedThreadStart threadStart = new ParameterizedThreadStart(sayHi); 29 Thread thread = new Thread(threadStart); 30 thread.Start("Fahim"); 31 thread.Join(); 32 33 threadStart = new ParameterizedThreadStart(new UsingThreadStart().sayHello); 34 thread = new Thread(threadStart); 35 thread.Start("Fahim"); 36 thread.Join(); 37 } 38}
1public class GreetingClass 2{ 3 private String name; 4 5 public GreetingClass(String name) 6 { 7 this.name = name; 8 } 9 10 public void sayHello() 11 { 12 Console.WriteLine("Hello " + name + " from instance method"); 13 } 14}
1GreetingClass gc = new GreetingClass("Fahim"); 2ThreadStart ts = new ThreadStart(gc.sayHello); 3Thread thread = new Thread(ts); 4thread.Start(); 5thread.Join();
1using System; 2using System.Threading; 3 4class Demonstration 5{ 6 static void Main() 7 { 8 GreetingClass gc = new GreetingClass("Fahim"); 9 ThreadStart ts = new ThreadStart(gc.sayHello); 10 Thread thread = new Thread(ts); 11 thread.Start(); 12 thread.Join(); 13 } 14} 15 16public class GreetingClass 17{ 18 private String name; 19 20 public GreetingClass(String name) 21 { 22 this.name = name; 23 } 24 25 public void sayHello() 26 { 27 Console.WriteLine("Hello " + name + " from instance method"); 28 } 29}
1using System; 2using System.Threading; 3 4class Demonstration 5{ 6 static void Main() 7 { 8 RetrieveResults retrieveResults = new RetrieveResults(10, (double res) => 9 { 10 Console.WriteLine("Result = " + res); 11 }); 12 13 ThreadStart ts = new ThreadStart(retrieveResults.square); 14 Thread thread = new Thread(ts); 15 16 thread.Start(); 17 thread.Join(); 18 } 19} 20 21// 定义回调方法签名的委托 22public delegate void ResultCallback(double result); 23 24public class RetrieveResults 25{ 26 private int x; 27 ResultCallback callback; 28 29 public RetrieveResults(int x, ResultCallback callback) 30 { 31 this.x = x; 32 this.callback = callback; 33 } 34 35 public void square() 36 { 37 double result = x * x; 38 39 if (callback != null) 40 callback(result); 41 } 42}

基础

线程池与线程中止(concept)