Part 86   Multithreading in C#

What is a Process:

Process is what the operating system uses to facilitate(帮助) the execution of a program by providing the resources required.Each process has unique process Id associated with(关联) it. You can view the process within which a program is being executed using windows task manager.

What is Thread:

Thread is a light weight(轻量级) process.A process has at least one thread which is commonly called(通常被称为) as main thread which actually executes the application code. A single process can hava multiple threads.

Please Note: All the threading related classes are present in(存在于) System.Threading namespace.

Part 87   Advantages and disadvantages of multithreading

Advantages of multithreading:

1, To maintain a responsive user interface(快速响应用户界面)

2, To make effcient use of processor time while waiting for I/O operations to complete.(适当的利用处理器,在等待I / O操作完成的这段时间。)

3, To split large, CPU-bound tasks to be processed simultaneously on a machine that has multiple processors/cores(分割大cpu密集型任务处理的机器上同时有多个处理器/核心)

Disadvantages of multithreading:

1, On a single processor/core machine threading can affect performance negatively as there is overhead involved with context-switching.(在单个处理器/核心的机器,线程会对上下文切换开销的性能有负面影响)

2, Have to write more lines of code to accomplish the same task.(需要编写更多的代码来完成相同的任务)

3,Multithreaded applications are difficult to write, understand, debug and maintain.(多线程应用程序很难写,理解、调试和维护。)

Please Note: Only use multithreading when the advantages of doing so outweigh the disavantages.

Part 88   ThreadStart delegate

To create a Thread, create an instance of Thread class and to it's constructor pass the name of the function that we want the thread to execute.

class Program
{
static void Main(string[] args)
{
Thread t = new Thread(Number.Print);
t.Start();
}
} public class Number
{
public static void Print()
{
for(int i=;i<;i++)
{
Console.WriteLine(i);
}
}
}

Thread

Thread t = new Thread(Number.Print);
 t.Start();

We can rewrite the above line using ThreadStart delegate as shown below

Thead t1 = new Thread(new ThreadStart(Number.Print));

t1.Start();

Why a delegate need to be passed as a parameter to the Thread class constructor?

The purpose of creating a Thread is to execute a function. A delegate is a type safe function pointer, meaning it points to a function that the thread has to execute. In short, all threads require an entry point to start execution. Any thread you create will need an explicitly defined entry point i.e(那就是说) a pointer to the function where they should begin execution. So threads always require a delegate.

In the code below, we are not explicitly creating the ThreadStart delegage, then how is it working here?

Thread t1 = new Thread(Number.Print);

t1.Start();

It's working in spite of(尽管) not creating the ThreadStart delegage explictly because the framework is doing it automatically for us.

Other ways, We can also rewrite the same line using delegate() keyword

Thread t = new Thread(delegate(){Number.Print();});

The same line rewritten using lambda expression

Thread t = new Thread(()=>Number.Print());

Part 86 to 88 Talking about Multithreading in C#的更多相关文章

  1. [转载] COM 套间

    http://www.vckbase.com/index.php/wv/1315 简序 大学毕业前的最后一学期,在一家公司实习,当时的工作需要用到一些操作系统提供的组件.那时候只知道COM这个名词,并 ...

  2. CUDA学习笔记(一)【转】

    CUDA编程中,习惯称CPU为Host,GPU为Device.编程中最开始接触的东西恐怕是并行架构,诸如Grid.Block的区别会让人一头雾水,我所看的书上所讲述的内容比较抽象,对这些概念的内容没有 ...

  3. (zhuan) How to Train Neural Networks With Backpropagation

    this blog from: http://blog.demofox.org/2017/03/09/how-to-train-neural-networks-with-backpropagation ...

  4. Java实现FTP批量大文件上传下载篇1

    本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量.大文件的上传下载控件.文章在比较了一系列FTP客户库的基础上,就其中一个比 ...

  5. [转帖]select提高并发,select和poll、epoll的区别(杂)

    同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. https://www.2cto.com/kf/20161 ...

  6. 线程模型、pthread 系列函数 和 简单多线程服务器端程序

    一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属于1:1模型. (一).N:1用户线程模型 “线程实现”建立在“进程控制”机制之上,由用 ...

  7. Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介

    Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...

  8. 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)

    并发编程概述   前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...

  9. CYQ.Data、ASP.NET Aries 百家企业使用名单

    如果您或您所在的公司正在使用此框架,请联系左侧的扣扣,告知我信息,我将为您添加链接: 以下内容为已反馈的用户,(收集始于:2016-08-08),仅展示99家: 序号 企业名称 企业网址 备注 1 山 ...

随机推荐

  1. SCCM客户端推送/卸载

    站点配置--站点--层次结构设置--自动批准客户端 ccm客户端安装日志:服务器端的ccm.log安装ccm客户端,需要添加一个具有访问客户端$admin权限的账户,如下:站点名称-右键 客户端安装设 ...

  2. 减少远程ssh的延迟

    今天搞了个很廉价的vps,ssh上去之后操作卡顿得不行,有时候输入一行命令后需要等五六秒才显示出来,蛋疼得不行. 然后想找一个解决方案,先是看到了mosh. 搜索了下教程,看了下说明,因为我这个廉价的 ...

  3. 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因

    声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...

  4. 【JavaScript】重温Javascript继承机制

    上段时间,团队内部有过好几次给力的分享,这里对西风师傅分享的继承机制稍作整理一下,适当加了些口语化的描述,留作备案. 一.讲个故事吧 澄清在先,Java和Javascript是雷锋和雷峰塔的关系.Ja ...

  5. [Ramda] Filter, Reject and Partition

    We'll learn how to get a subset of an array by specifying items to include with filter, or items to ...

  6. [ES6] 13. Using the ES6 spread operator ...

    The spread operator (...) allows you to "explode" an array into its individual elements. S ...

  7. iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)

    iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)   这里我们就直接上实例: 一:新建一个项目singleV ...

  8. 判断null

    var tmp = this.pullDown.getValue();   if(!tmp && typeof(tmp)!="undefined" &&am ...

  9. 对lua继承中self.__index = self的释疑

    首先看看从lua表中查找一个键时的流程: -- 当从表t中查找键k时,lua处理如下: -- 1.t中是否有k,有则直接返回值,否则第2步 -- 2.t是否有元表, 无则返回nil, 有则第3步 -- ...

  10. 从零开始学JAVA(04)-连接数据库MSSQL(JDBC准备篇)

    在JAVA中可以使用JDBC连接数据库,不管是哪种数据库,首先必须下载驱动,包括Windows的MSSQL. 1.下载MSSQL的JDBC驱动,可以通过百度“Microsoft JDBC Driver ...