Part 86 to 88 Talking about Multithreading in C#
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#的更多相关文章
- [转载] COM 套间
http://www.vckbase.com/index.php/wv/1315 简序 大学毕业前的最后一学期,在一家公司实习,当时的工作需要用到一些操作系统提供的组件.那时候只知道COM这个名词,并 ...
- CUDA学习笔记(一)【转】
CUDA编程中,习惯称CPU为Host,GPU为Device.编程中最开始接触的东西恐怕是并行架构,诸如Grid.Block的区别会让人一头雾水,我所看的书上所讲述的内容比较抽象,对这些概念的内容没有 ...
- (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 ...
- Java实现FTP批量大文件上传下载篇1
本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量.大文件的上传下载控件.文章在比较了一系列FTP客户库的基础上,就其中一个比 ...
- [转帖]select提高并发,select和poll、epoll的区别(杂)
同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. https://www.2cto.com/kf/20161 ...
- 线程模型、pthread 系列函数 和 简单多线程服务器端程序
一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属于1:1模型. (一).N:1用户线程模型 “线程实现”建立在“进程控制”机制之上,由用 ...
- Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介
Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- CYQ.Data、ASP.NET Aries 百家企业使用名单
如果您或您所在的公司正在使用此框架,请联系左侧的扣扣,告知我信息,我将为您添加链接: 以下内容为已反馈的用户,(收集始于:2016-08-08),仅展示99家: 序号 企业名称 企业网址 备注 1 山 ...
随机推荐
- jcmd命令使用
概述 在JDK 1.7之后,新增了一个命令行工具jcmd. 它是一个多功能工具,能够用来导出堆,查看java进程,导出线程信息.运行GC等. 使用演示样例 以下这个命令能够列出当前运行的全部虚拟机: ...
- linux系统命令解析(0基础篇)
经常使用命令 ctrl+W+T 当进入gvim文档编辑界面后,假设是C文件,能够列出当前文件里的全部函数. Shift+* 当进入gvim文档编辑页面,能够搜索当前keyword. ech ...
- 编写你自己的单点登录(SSO)服务
王昱 yuwang881@gmail.com 博客地址http://yuwang881.blog.sohu.com 摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统其中.本文从 ...
- C#操作Excel(2)-- 打开-读取Excel文档
由于要为某软件实现导出Excel功能,故有此文. 本文的开发环境是Visual Studio 2010 ,C#, Excel 2007. 新建C#工程后打开Solution Explorer,可以看到 ...
- Android中显示网页的多种方式
在android中显示页面主要有两种方式,一种是在Activity里面直接显示网页,另一种是调用浏览器显示网页.方式不同,使用的方法也不同,下面我们分别讲解. 一.在Activity里面直接显示网页 ...
- 《计算机问题求解》总结——2014年CCF计算机课程改革导教班(2014.07.11)
一:引言 "心想事成".这是自己获得导教班学习机会的最佳概括.2013年年末学习李晓明老师的<人群与网络>课程:随后网络认识烟台大学贺利坚老师,了解到2013年 ...
- 关于Web端即JS端编程
主要的技术是 HTML/JS/CSS/XML Web就是JS/DOM编程. 页面的数据来源: XML, JSON, HTML, Text, 第三方页面或者数据. 不一定都要跟服务器进行交互. JS端 ...
- C# Redis分布式缓存
C# Redis实战(七) 七.修改数据 在上一篇 C# Redis实战(六)中介绍了如何查询Redis中数据,本篇将介绍如何修改Redis中相关数据.大家都知道Redis是key-value型存储系 ...
- jq实现搜索引擎的提示效果
(function ($) { $.fn.Search = function (options) { var defaults = { inputid: "search", div ...
- 关于 Android项目“error: Apostrophe not preceded by \ (”的解决方法
用Eclipse环境开发Android项目,如果编译时控制台报出“error: Apostrophe not preceded by \ (”这种错误,那么多半是因为项目中的一个strings.xml ...