using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
//using System.Diagnostics;namespace 多线程
{#region 我的程序class ResultCount{public static int PassNumber { get; set; }public static int FailNumber { get; set; }}class Program{private static object _lock = new object();static void Main(string[] args){Thread[] threadArray = new Thread[10];for (int i = 0; i < 10; i++){threadArray[i] = new Thread(new ThreadStart(PassCountAdd));}foreach (var item in threadArray){item.Start();}foreach (var item in threadArray){//Join: 一个同步方法,该方法阻止调用线程 (即,调用方法的线程) ,直到 Join 调用方法的线程完成。//使用此方法可以确保线程已终止。 如果线程未终止,调用方将无限期阻止。 在下面的示例中,//Thread1 线程调用的 Join() 方法 Thread2 ,这会导致 Thread1 一直阻止到 Thread2 完成为止。item.Join();}Console.WriteLine(ResultCount.PassNumber);Console.WriteLine(ResultCount.FailNumber);}static void PassCountAdd(){for (int i = 0; i < 100000; i++){lock (_lock){ResultCount.PassNumber++;ResultCount.FailNumber++;}}}}#endregion#region Microsoft官网例程//public class Example//{// static Thread thread1, thread2;// public static void Main()// {// thread1 = new Thread(new ThreadStart(ThreadProc));// thread1.Name = "Thread1";// thread1.Start();// thread2 = new Thread(new ThreadStart(ThreadProc));// thread2.Name = "Thread2";// thread2.Start();// }// private static void ThreadProc()// {// Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);// if (Thread.CurrentThread.Name == "Thread1"// && (thread2.ThreadState != ThreadState.Unstarted))// {// //Join: 一个同步方法,该方法阻止调用线程 (即,调用方法的线程) ,直到 Join 调用方法的线程完成。// //使用此方法可以确保线程已终止。 如果线程未终止,调用方将无限期阻止。 在下面的示例中,// //Thread1 线程调用的 Join() 方法 Thread2 ,这会导致 Thread1 一直阻止到 Thread2 完成为止。// thread2.Join();// }// Thread.Sleep(4000);// Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);// Console.WriteLine("Thread1: {0}", thread1.ThreadState);// Console.WriteLine("Thread2: {0}", thread2.ThreadState);// }//}#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;namespace ConsoleApplication1
{public static class MyLock //使用同一把锁对同一资源进行互斥访问保护{public static object _lock = new object();}class Program{static void Main(string[] args){Student stu1 = new Student(1, "Tom", ConsoleColor.Green);Student stu2 = new Student(2, "Jerry", ConsoleColor.Yellow);Student stu3 = new Student(3, "Jason", ConsoleColor.Red);#region 使用Action委托//Action action1 = new Action(stu1.DoHomeWork);//Action action2 = new Action(stu2.DoHomeWork);//Action action3 = new Action(stu3.DoHomeWork);//action1.BeginInvoke(null, null);//action2.BeginInvoke(null, null);//action3.BeginInvoke(null, null);#endregion#region Thread多线程//Thread thread1 = new Thread(new ThreadStart(stu1.DoHomeWork));//Thread thread2 = new Thread(new ThreadStart(stu2.DoHomeWork));//Thread thread3 = new Thread(new ThreadStart(stu3.DoHomeWork));//thread1.Start();//thread2.Start();//thread3.Start();#endregion#region Task多线程Task task1 = new Task(new Action(stu1.DoHomeWork));Task task2 = new Task(new Action(stu2.DoHomeWork));Task task3 = new Task(new Action(stu3.DoHomeWork));task1.Start();task2.Start();task3.Start();#endregionfor (int i = 0; i < 10; i++){lock(MyLock._lock){Console.ForegroundColor = ConsoleColor.White;Console.WriteLine("Main thread{0}!", i + 1);}Thread.Sleep(100);}}}class Student{public int Id { get; set; }public string Name { get; set; }public ConsoleColor Color { get; set; }public Student(int id, string name, ConsoleColor color){Id = id;Name = name;Color = color;}public void DoHomeWork(){for (int i = 0; i < 5; i++){lock(MyLock._lock){Console.ForegroundColor = Color;Console.WriteLine("Id:{0},Name:{1},Color:{2}, doing homework {3}hours.",Id, Name, Console.ForegroundColor, i + 1);}Thread.Sleep(100);}}}
}