命名空间:System.Threading
delegate 关键字用于声明一个引用类型,该引用类型可用于封装命名方法或匿名方法。委托类似于 C++ 中的函数指针;但是,委托是类型安全和可靠的。
注意:
public delegate void TestDelegate(string message); public delegate int TestDelegate(MyType m,long num); static void Add(int num1,int num2) { Console.Write(num1+num2); } delegate void Sum(int i,int j); public static void Main(string[] args) { Sum s=new Sum(Add);// Sum s=Add; s(1,2); UseDelegate(Sum s,int i,int j) } public static UseDelegate(Sum s,int i,int j) { i=i+1; s(i,j); } // Declare delegate -- defines required signature: delegate double MathAction(double num); class DelegateTest { // Regular method that matches signature: static double Double(double input) { return input * 2; } static void Main() { // Instantiate delegate with named method: MathAction ma = Double; // Invoke delegate ma: double multByTwo = ma(4.5); Console.WriteLine("multByTwo: {0}", multByTwo); // Instantiate delegate with anonymous method: MathAction ma2 = delegate(double input) { return input * input; }; double square = ma2(5); Console.WriteLine("square: {0}", square); // Instantiate delegate with lambda expression MathAction ma3 = s => s * s * s; double cube = ma3(4.375); Console.WriteLine("cube: {0}", cube); } // Output: // multByTwo: 9 // square: 25 // cube: 83.740234375 }
Instances of the Thread class represent either foreground threads or background threads. Background threads are identical to foreground threads with one exception: a background thread does not keep a process running if all foreground threads have terminated. Once all foreground threads have been stopped, the runtime stops all background threads and shuts down.
By default, the following threads execute in the foreground:
The main application thread.
All threads created by calling a Thread class constructor.
The following threads execute in the background by default:
Thread pool threads, which are a pool of worker threads maintained by the runtime. You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.
Thread(ParameterizedThreadStart),在线程开始的时候可以向委托传参。
Thread(ThreadStart),线程开始时不能传参,
注意带参的委托的参数类型为object,直接用函数名传入也可。
CurrentThread:Gets the currently running thread
IsAlive:Gets a value indicating the execution status of the current thread
IsBackground:Gets or sets a value indicating whether or not a thread is a background thread
IsThreadPoolThread:Gets a value indicating whether or not a thread belongs to the managed thread pool
ManagedThreadId:Gets a unique identifier(int) for the current managed thread.
Name:Gets or sets the name of the thread.This property is write-once. Because the default value of a thread's Name property is null, you can determine whether a name has already been explicitly assigned to the thread by comparing it with null.
Priority:Gets or sets a value indicating the scheduling priority of a thread
ThreadState:The ThreadState property provides more specific information than the IsAlive property.Unstarted/WaitSleepJoin/Stopped
Abort():
Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
Join():
Blocks the calling thread until the thread represented by this instance terminates, while continuing to perform standard COM and SendMessage pumping.
Join(int32):
Blocks the calling thread until the thread represented by this instance terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.
Resume():
Resumes a thread that has been suspended.过时了,慎用!
Sleep(Int32):
Suspends the current thread for the specified number of milliseconds
Start():
Causes the operating system to change the state of the current instance to Running.
Suspend():
Either suspends the thread, or if the thread is already suspended, has no effect.过时了,慎用!
// using System.Threading; class Account { private Object thisLock = new Object(); int balance; Random r = new Random(); public Account(int initial) { balance = initial; } int Withdraw(int amount) { // This condition never is true unless the lock statement // is commented out. if (balance < 0) { throw new Exception("Negative Balance"); } // Comment out the next line to see the effect of leaving out // the lock keyword. lock (thisLock) { if (balance >= amount) { Console.WriteLine("Balance before Withdrawal : " + balance); Console.WriteLine("Amount to Withdraw : -" + amount); balance = balance - amount; Console.WriteLine("Balance after Withdrawal : " + balance); return amount; } else { return 0; // transaction rejected } } } public void DoTransactions() { for (int i = 0; i < 100; i++) { Withdraw(r.Next(1, 100)); } } } class Test { static void Main() { Thread[] threads = new Thread[10]; Account acc = new Account(1000); for (int i = 0; i < 10; i++) { Thread t = new Thread(new ThreadStart(acc.DoTransactions)); threads[i] = t; } for (int i = 0; i < 10; i++) { threads[i].Start(); } //block main thread until all other threads have ran to completion. foreach (var t in threads) t.Join(); } }