前言

学习C#两个多月了,像当初实习做PHP开发一样,也是由着一个个Feature需求,慢慢掌握了很多相关的编程技巧。本次主要记录下学习C# 多线程的相关知识。

参考书籍:《Csharp高级编程(第7版)》

1.使用线程的原因

不过运行多个线程也要注意一些问题:他们可以同时运行,但是如果线程访问相同的数据,就很容易出问题,必须实现同步机制

 2.理解线程

线程是程序中独立的指令流。C#编写的程序都有一个入口点(即Main()方法),程序从该方法的第一条开始执行,直到方法返回为止。这种程序结构非常适合用于一个有任务序列的程序,但是程序常常需要同时完成多个任务。

这就要用到多个线程,比如Word的拼写检查器的工作原理是这样的:一个线程等待用户输入,另一个线程进行后台搜索,第3个线程将写入的数据保存在临时文件中,第4个线程从Internet上下载其他数据。

理解线程很重要的一点其实是理解线程和进程的关系:

3.创建线程的方式

  • 异步委托

创建线程的一种简单地方式是定义委托,并异步调用它。委托是方法的类型安全的引用,它使用线程池来完成异步任务。

代码实例:

使用投票的例子,并检查委托是否完成了它的任务。等待异步委托结果的四种方式:

(1)轮询

Delegate类提供了BeginInvoke()方法,通过其返回类型IAsyncResult ,可以获取委托的相关信息,并检验它是否完成了任务。只要委托没有完成其任务,程序的主线程就继续执行while循环。

(2)等待句柄

使用与IAsyncResult 相关联的等待句柄。使用AsyncWaitHandle属性可以访问等待句柄,该属性可以返回一个WaitHandle类型的对象,它可以等待委托线程完成其任务。

(3)异步回调

(4)Lambda表达式 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics; namespace Wrox.ProCSharp.Threading
{
class Program
{
static int TakesAWhile(int data, int ms)
{
Console.WriteLine("TakesAWhile started");
Thread.Sleep(ms);
Console.WriteLine("TakesAWhile completed");
return ++data;
}

     //要从委托中调用这个方法,必须定义一个有相同参数和返回类型的的委托
public delegate int TakesAWhileDelegate(int data, int ms); static void Main()
{
// synchronous
// TakesAWhile(1, 3000);
TakesAWhileDelegate d1 = TakesAWhile; // (1)polling轮询
//IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);
//while (!ar.IsCompleted)
//{
// // doing something else
// Console.Write(".");
// Thread.Sleep(50);
//}
//int result = d1.EndInvoke(ar);
//Console.WriteLine("result: {0}", result); // (2)wait handle
//IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);
//while (true)
//{
// Console.Write(".");
// if (ar.AsyncWaitHandle.WaitOne(50, false))
// {
// Console.WriteLine("Can get the result now");
// break;
// }
//}
//int result = d1.EndInvoke(ar);
//Console.WriteLine("result: {0}", result); // (3)async callback
//d1.BeginInvoke(1, 3000, TakesAWhileCompleted, d1);
//for (int i = 0; i < 100; i++)
//{
// Console.Write(".");
// Thread.Sleep(50);
//} //(4)Lambda expression:可以直接访问作用域外的变量d1,所以不需要把一个值赋予BeginInvoke()方法的最后一个参数
d1.BeginInvoke(1, 3000,
ar =>
{
int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
},
null);
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
} } static void TakesAWhileCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar"); TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;
Trace.Assert(d1 != null, "Invalid object type"); int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
}
}
}

运行结果:

代码实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; namespace Wrox.ProCSharp.Threading
{
public class MyThread
{
private string data; public MyThread(string data)
{
this.data = data;
} public void ThreadMain()
{
Console.WriteLine("Running in a thread, data: {0}", data);
}
} public struct Data
{
public string Message;
} class Program
{
static void Main()
{
FirstThread(); //var t1 = new Thread(Prio);
//t1.Name = "First"; //var t2 = new Thread(Prio);
//t2.Name = "Second";
//t1.Priority = ThreadPriority.Highest;
//t2.Priority = ThreadPriority.Lowest; //t1.Start();
//t2.Start(); //var t1 = new Thread(ThreadMain);
//t1.Name = "MyNewThread1";
//t1.IsBackground = true;
//t1.Start();
//Console.WriteLine("Main thread ending now..."); //var d = new Data { Message = "Info" };
//var t2 = new Thread(ThreadMainWithParameters);
//t2.Start(d); //var obj = new MyThread("info");
//var t3 = new Thread(obj.ThreadMain);
//t3.Start();
} static void Prio()
{
for (int i = 0; i < 10000; i++)
{
Console.WriteLine("{0}, {1}", Thread.CurrentThread.Name, i);
}
} static void ThreadMain()
{
Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name);
Thread.Sleep(3000);
// Console.WriteLine("Running in the thread {0}, id: {1}.", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name);
} static void ThreadMainWithParameters(object o)
{
Data d = (Data)o;
Console.WriteLine("Running in a thread, received {0}", d.Message);
} static void FirstThread()
{
new Thread(() => Console.WriteLine("Running in a thread, id: {0}", Thread.CurrentThread.ManagedThreadId)).Start(); Console.WriteLine("This is the main thread, id: {0}", Thread.CurrentThread.ManagedThreadId);
}
}
}

运行结果:

代码实例:

using System;
using System.Threading; namespace Wrox.ProCSharp.Threading
{
class Program
{
static void Main()
{
int nWorkerThreads;
int nCompletionPortThreads;
ThreadPool.GetMaxThreads(out nWorkerThreads, out nCompletionPortThreads);
Console.WriteLine("Max worker threads: {0}, I/O completion threads: {1}", nWorkerThreads, nCompletionPortThreads); for (int i = 0; i < 5; i++)
{
ThreadPool.QueueUserWorkItem(JobForAThread); } Thread.Sleep(3000);
} static void JobForAThread(object state)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("loop {0}, running inside pooled thread {1}", i,
Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(50);
} }
}
}

运行结果:

代码实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO; namespace TaskSamples
{
class Program
{
static void Main()
{
// ParallelDemo(); SimpleTask();
// ContinuationTask();
// ParentAndChild();
// ResultsFromTasks(); Thread.Sleep(5000); //ParentAndChild(); // HierarchyTasks("c:\\"); //Parallel.f
//Task t1 = new Task(() => Console.WriteLine("running in a task"));
//Task t2 = new Task(() => Console.WriteLine("running in a task")); //for (int i = 0; i < 10; i++)
//{
// Task t1 = new Task(o =>
// {
// Console.WriteLine("running in a task {0}", Thread.CurrentThread.ManagedThreadId);
// Thread.Sleep(500);
// Console.WriteLine("still running {0}", Thread.CurrentThread.ManagedThreadId);
// }, "data", TaskCreationOptions.None);
// // t1.RunSynchronously();
// t1.Start(); //} //Console.WriteLine("start sleep main");
//Thread.Sleep(3000);
//Console.WriteLine("main thread");
} static void ResultsFromTasks()
{
var t1 = new Task<Tuple<int,int>>(TaskWithResult, Tuple.Create<int, int>(8, 3));
t1.Start();
Console.WriteLine(t1.Result);
t1.Wait();
Console.WriteLine("result from task: {0} {1}", t1.Result.Item1, t1.Result.Item2);
} static Tuple<int, int> TaskWithResult(object division)
{
Tuple<int, int> div = (Tuple<int, int>)division;
int result = div.Item1 / div.Item2;
int reminder = div.Item1 % div.Item2;
Console.WriteLine("task creates a result..."); return Tuple.Create<int, int>(result, reminder);
} static void SimpleTask()
{
// using task factory
TaskFactory tf = new TaskFactory();
Task t1 = tf.StartNew(TaskMethod); // using the task factory via a task
Task t2 = Task.Factory.StartNew(TaskMethod); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); // using Task constructor
Task t3 = new Task(TaskMethod);
// t3.Start();
t3.RunSynchronously(); Task t4 = new Task(TaskMethod, TaskCreationOptions.PreferFairness);
t4.Start(); } static void ContinuationTask()
{
Task t1 = new Task(DoOnFirst);
Task t2 = t1.ContinueWith(DoOnSecond);
Task t3 = t1.ContinueWith(DoOnSecond);
Task t4 = t2.ContinueWith(DoOnSecond);
Task t5 = t1.ContinueWith(DoOnError, TaskContinuationOptions.OnlyOnFaulted);
t1.Start(); Thread.Sleep(5000); } static void DoOnFirst()
{
Console.WriteLine("doing some task {0}", Task.CurrentId);
Thread.Sleep(3000);
} static void DoOnSecond(Task t)
{
Console.WriteLine("task {0} finished", t.Id);
Console.WriteLine("this task id {0}", Task.CurrentId);
Console.WriteLine("do some cleanup");
Thread.Sleep(3000);
} static void DoOnError(Task t)
{
Console.WriteLine("task {0} had an error!", t.Id);
Console.WriteLine("my id {0}", Task.CurrentId);
Console.WriteLine("do some cleanup");
} static void ParentAndChild()
{
Task parent = new Task(ParentTask);
parent.Start();
Thread.Sleep(2000);
Console.WriteLine(parent.Status);
Thread.Sleep(4000);
Console.WriteLine(parent.Status); }
static void ParentTask()
{
Console.WriteLine("task id {0}", Task.CurrentId);
Task child = new Task(ChildTask); // , TaskCreationOptions.DetachedFromParent);
child.Start();
Thread.Sleep(1000);
Console.WriteLine("parent started child");
// Thread.Sleep(3000);
}
static void ChildTask()
{
// Console.WriteLine("task id {0}, parent: {1}", Task.Current.Id, Task.Current.Parent.Id);
Console.WriteLine("child");
Thread.Sleep(5000);
Console.WriteLine("child finished");
} static void TaskMethod()
{
Console.WriteLine("running in a task");
Console.WriteLine("Task id: {0} {1}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
} static void ParallelDemo()
{
// Parallel.For(0, 5, i => Console.WriteLine(i));
Parallel.For<string>(0, 20, () => "abcd",
(x, ls, str) =>
{
Console.WriteLine(x);
return "defg";
},
(str) =>
{
Console.WriteLine("action {0}", str);
}); ParallelOptions po = new ParallelOptions(); } //static void ParentAndChild()
//{
// TaskFactory factory = new TaskFactory();
// var t1 = factory.StartNew(() =>
// {
// Console.WriteLine("parent task {0}", Task.CurrentId); // factory.StartNew(() =>
// {
// Console.WriteLine("child task {0}", Task.CurrentId);
// Thread.Sleep(2000);
// Console.WriteLine("finished child");
// }, TaskCreationOptions.AttachedToParent); // Console.WriteLine("finished parent");
// }); // t1.Wait(); //} }
}

运行结果:

代码实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Wrox.ProCSharp.Threading
{
class Program
{
static void Main()
{
//ParallelFor();
//ParallelForeach();
ParallelInvoke();
} static void ParallelInvoke()
{
//并行运行多个任务:Parallel.Invoke()方法允许传递一个Action委托数组,其中指定应运行的方法
Parallel.Invoke(Foo, Bar);
} static void Foo()
{
Console.WriteLine("foo");
} static void Bar()
{
Console.WriteLine("bar");
} static void ParallelForeach()
{
string[] data = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}; //异步方式遍历
ParallelLoopResult result =
Parallel.ForEach<string>(data, s =>
{
Console.WriteLine(s);
}); Parallel.ForEach<string>(data,
(s, pls, l) =>
{
Console.WriteLine("{0} {1}", s, l); }); } static void ParallelFor()
{
//ParallelLoopResult result =
// Parallel.For(0, 10, i =>
// {
// Console.WriteLine("{0}, task: {1}, thread: {2}", i,
// Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
// Thread.Sleep(10); // });
//Console.WriteLine(result.IsCompleted); //ParallelLoopResult result =
// Parallel.For(10, 40, (int i, ParallelLoopState pls) =>
// {
// Console.WriteLine("i: {0} task {1}", i, Task.CurrentId);
// Thread.Sleep(10);
// if (i > 15)
// pls.Break();
// });
//Console.WriteLine(result.IsCompleted);
//Console.WriteLine("lowest break iteration: {0}", result.LowestBreakIteration); Parallel.For<string>(0, 20,
() =>
{
// invoked once for each thread
Console.WriteLine("init thread {0}, task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
return String.Format("t{0}", Thread.CurrentThread.ManagedThreadId);
},
(i, pls, str1) =>
{
// invoked for each member
Console.WriteLine("body i {0} str1 {1} thread {2} task {3}", i, str1,
Thread.CurrentThread.ManagedThreadId,
Task.CurrentId);
Thread.Sleep(10);
return String.Format("i {0}", i); },
(str1) =>
{
// final action on each thread
Console.WriteLine("finally {0}", str1);
}); }
}
}

运行结果:

代码实例:

  

  

  

ps:在运行书中附带的sample时,报错:

经查证,发现这是由于StartupUri中的内容与窗口名称不一致所导致。

这部分的知识可以参考一篇译文:WPF教程(十)使用App.xaml

C#读书笔记:线程,任务和同步的更多相关文章

  1. 读书笔记—CLR via C#同步构造28-29章节

    前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...

  2. 【读书笔记】iOS网络-同步请求,队列式异步请求,异步请求的区别

    一,同步请求的最佳实践. 1,只在后台过程中使用同步请求,除非确定访问的是本地文件资源,否则请不要在主线程上使用. 2,只有在知道返回的数据不会超出应用的内存时才使用同步请求.记住,整个响应体都会位于 ...

  3. 【读书笔记】iOS-网络-同步请求,队列式异步请求,异步请求的区别

    一,同步请求的最佳实践. 1,只在后台过程中使用同步请求,除非确定访问的是本地文件资源,否则请不要在主线程上使用. 2,只有在知道返回的数据不会超出应用的内存时才使用同步请求.记住,整个响应体都会位于 ...

  4. Clr Via C#读书笔记---线程基础

    趣闻:我是一个线程:http://kb.cnblogs.com/page/542462/ 进程与线程 进程:应用程序的一个实例使用的资源的集合.每个进程都被赋予了一个虚拟地址空间. 线程:对CPU进行 ...

  5. OCA读书笔记(9) - 管理数据同步

    9.Managing Data Concurrency 描述锁机制以及oracle如何管理数据一致性监控和解决锁冲突 管理数据的并发--管理锁数据的不一致:脏读更改丢失幻影读 脏读:数据是指事务T2修 ...

  6. Java并发读书笔记:线程安全与互斥同步

    目录 导致线程不安全的原因 什么是线程安全 不可变 绝对线程安全 相对线程安全 线程兼容 线程对立 互斥同步实现线程安全 synchronized内置锁 锁即对象 是否要释放锁 实现原理 啥是重进入? ...

  7. <<操作系统精髓与设计原理>>读书笔记(一) 并发性:互斥与同步(1)

    <<操作系统精髓与设计原理>>读书笔记(一) 并发性:互斥与同步 并发问题是所有问题的基础,也是操作系统设计的基础.并发包括很多设计问题,其中有进程间通信,资源共享与竞争,多个 ...

  8. 《深入了解java虚拟机》高效并发读书笔记——Java内存模型,线程,线程安全 与锁优化

    <深入了解java虚拟机>高效并发读书笔记--Java内存模型,线程,线程安全 与锁优化 本文主要参考<深入了解java虚拟机>高效并发章节 关于锁升级,偏向锁,轻量级锁参考& ...

  9. windows线程池四种情形(win核心读书笔记)

    windows线程池四种情形(win核心读书笔记) Mircosoft从Windows2000引入线程池API,并在Vista后对线程池重新构架,引入新的线程池API.以下所有线程池函数均适用于Vis ...

  10. 副本机制与副本同步------《Designing Data-Intensive Applications》读书笔记6

    进入到第五章了,来到了分布式系统之中最核心与复杂的内容:副本与一致性.通常分布式系统会通过网络连接的多台机器上保存相同数据的副本,所以在本篇之中,我们来展开看看如何去管理和维护这些副本,以及这个过程之 ...

随机推荐

  1. Cglib动态代理实现方式

    Cglib动态代理实现方式 我们先通过一个demo看一下Cglib是如何实现动态代理的. 首先定义个服务类,有两个方法并且其中一个方法用final来修饰. public class PersonSer ...

  2. [python] A*算法基于栅格地图的全局路径规划

    # 所有节点的g值并没有初始化为无穷大 # 当两个子节点的f值一样时,程序选择最先搜索到的一个作为父节点加入closed # 对相同数值的不同对待,导致不同版本的A*算法找到等长的不同路径 # 最后c ...

  3. 第8章 java中的并发工具类

    8.1 等待线程完成的CountDownLatch 作用:让一个线程等待其余线程完成之后在继续执行,如主线程等待开启服务的子线程执行完毕后主线程继续执行,类似于join.

  4. @ConfigurationProperties 配置详解

    文章转自 https://blog.csdn.net/qq_26000415/article/details/78942494 前言新的一年到了,在这里先祝大家新年快乐.我们在上一篇spring bo ...

  5. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  6. fastjson 返回json字符串,JSON.parse 报错

    这是由于转义字符引起的如 : \ , fastjson 处理后是双反斜杠:\\ ,而 JSON.parse 解析时需要4个反斜杠 ,即 js解析json 反斜杠时,需要 4个 解成 1 个 解决方法: ...

  7. ASP.NET 管道

    序号 名称 说明 1 BeginRequest ASP.NET开始处理的第一个时间,表示处理的开始 2 AuthenticateRequest 验证请求,一般用来取得请求的用户信息 3 PostAut ...

  8. 《HTTP协议:菜鸟入门系列》

    很多测试人员在有了一定的测试经验(一般是1-2年)后,就会陷入瓶颈阶段,想提升,但不知道如何提升,学习又没有比较明确的方向,曾经我也是... 那么,我建议系统的学习一下HTTP协议,好处很多:对接口测 ...

  9. 修改tomcat JVM 大小

    参考:https://blog.csdn.net/mynamepg/article/details/80591348 1.Xms表示初始分配给jvm的内存大小,-Xmx表示最大可分配给jvm的内存大小 ...

  10. keystone系列二:HTTP协议

    一 为何要学习HTTP协议 http协议就是通信的双方共同遵守的标准,就好比要合伙办事的两家公司签署的合同. openstack中各组件是基于restful api通信的,restful api可以单 ...