C# 任务、线程、同步(一)
1 static object taskMethodLock = new object(); 2 static void TaskMethod(object title) 3 { 4 lock (taskMethodLock) 5 { 6 Console.WriteLine(title); 7 Console.WriteLine("Task id: {0}, thread: {1}", 8 Task.CurrentId == null ? "no task" : Task.CurrentId.ToString(), 9 Thread.CurrentThread.ManagedThreadId); 10 Console.WriteLine("is pooled thread: {0}", Thread.CurrentThread.IsThreadPoolThread); 11 Console.WriteLine("is background thread: {0}", Thread.CurrentThread.IsBackground); 12 Console.WriteLine(); 13 } 14 }
1、线程池中的任务
1 var tf = new TaskFactory(); 2 Task t1 = tf.StartNew(TaskMethod, "using a task factory"); 3 4 Task t2 = Task.Factory.StartNew(TaskMethod, "factory via a task"); 5 6 var t3 = new Task(TaskMethod, "using a task constructor and Start"); 7 t3.Start(); 8 9 Task t4 = Task.Run(() => TaskMethod("using the Run method"));
2、同步任务
TaskMethod("just the main thread"); var t1 = new Task(TaskMethod, "run sync"); t1.RunSynchronously();
3、使用单独线程的任务
var t1 = new Task(TaskMethod, "long running",TaskCreationOptions.LongRunning); t1.Start();
4、Future 任务结果
1 static void ResultsFromTasks() 2 { 3 var t1 = new Task<Tuple<int, int>>(TaskWithResult, Tuple.Create<int, int>(8, 3)); 4 t1.Start(); 5 Console.WriteLine(t1.Result); 6 7 t1.Wait(); 8 9 Console.WriteLine("result from task: {0} {1}", t1.Result.Item1, t1.Result.Item2); 10 } 11 static Tuple<int, int> TaskWithResult(object division) 12 { 13 Tuple<int, int> div = (Tuple<int, int>)division; 14 int result = div.Item1 / div.Item2; 15 int reminder = div.Item1 % div.Item2; 16 Console.WriteLine("task creates a result ..."); 17 18 return Tuple.Create<int, int>(result, reminder); 19 }
5、连续任务
1 static void ContinuationTask() 2 { 3 Task t1 = new Task(DoOnFirst); 4 Task t2 = t1.ContinueWith(DoOnSecond); 5 Task t3 = t1.ContinueWith(DoOnSecond); 6 Task t4 = t1.ContinueWith(DoOnSecond); 7 Task t5 = t1.ContinueWith(DoOnError, TaskContinuationOptions.OnlyOnFaulted); 8 t1.Start(); 9 10 Thread.Sleep(5000); 11 12 } 13 static void DoOnFirst() 14 { 15 Console.WriteLine("doing some task {0}", Task.CurrentId); 16 Thread.Sleep(2000); 17 } 18 static void DoOnSecond(Task t) 19 { 20 Console.WriteLine("task {0} finished", t.Id); 21 Console.WriteLine("this task id {0}", Task.CurrentId); 22 Console.WriteLine("do some cleanup"); 23 Thread.Sleep(3000); 24 } 25 static void DoOnError(Task t) 26 { 27 Console.WriteLine("task {0} had an error!", t.Id); 28 Console.WriteLine("my id {0}", Task.CurrentId); 29 Console.WriteLine("do some cleanup"); 30 }
6、任务层次结构
1 static void ParentAndChild() 2 { 3 var parent = new Task(ParentTask); 4 parent.Start(); 5 Thread.Sleep(2000); 6 Console.WriteLine(parent.Status); 7 Thread.Sleep(4000); 8 Console.WriteLine(parent.Status); 9 10 } 11 static void ParentTask() 12 { 13 Console.WriteLine("task id {0}", Task.CurrentId); 14 var child = new Task(ChildTask); // , TaskCreationOptions.DetachedFromParent); 15 child.Start(); 16 Thread.Sleep(1000); 17 Console.WriteLine("parent started child"); 18 // Thread.Sleep(3000); 19 } 20 static void ChildTask() 21 { 22 // Console.WriteLine("task id {0}, parent: {1}", Task.Current.Id, Task.Current.Parent.Id); 23 Console.WriteLine("child"); 24 Thread.Sleep(5000); 25 Console.WriteLine("child finished"); 26 }
鹰击长空,鱼翔浅底