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 山 ...
随机推荐
- SCOM资源池
为了分散RMS特定的工作负载,默认创建有三个资源池. 1. All Management Servers Resource Pool: 将大多数RMS具体实例和工作流放入到这个池中.默认情况下,如果一 ...
- Java学习笔记之深入理解引用
引言:Java中数据传递的方式,除了基本数据类型是按照值传递,其它类型全部是按照引用传递,这和C++有很大区别,但是很多网上文章都解释的不清楚,甚至是错误的,在查阅资料之后,下面整理出一个比较容易理解 ...
- hihocoder #1179 : 永恒游戏 暴力
#1179 : 永恒游戏 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/11 ...
- Approaching the Fun Factor in Game Design
I recently did some research on this and talked to Dr. Clayton Lewis (computer Scientist in Residenc ...
- pylons使用多个数据库(multiple DB)
最近做的工程要修改成两个数据库的,一个测试数据库, 一个线上数据库. 所以就要把原来的只有一个数据库的改成两个数据库. 第一步:修改development.ini # SQLAlchemy datab ...
- ios实例开发精品源码文章推荐
iOS源码:游戏引擎-推箱子游戏 http://www.apkbus.com/android-106392-1-11.html iOS源码:进度条-Colorful ProgressView http ...
- jQuery 的 live() 方法对 hover 事件的处理
因为hover不是标准的事件,因此无法直接使用live进行处理,故使用以下方法代替,效果一样 <script type="text/javascript"> $(&qu ...
- mysql 线程级别的缓冲区
线程栈信息使用内存(thread_stack) 主要用来存放每一个线程自身的标识信息,如线程id,线程运行时基本信息等等,我们可以通过 thread_stack 参数来设置为每一个线程栈分配多大的内存 ...
- Python编码相关文章推荐
Table of Contents 1. 分享 分享 python2里面编码对很多人来说是个很头疼的问题,今天分享一篇编码相关的文章,是前几天读到的相比于大部分解释编码问题的一知半解模糊不清,这篇写得 ...
- 解析 iOS 动画原理与实现
这篇文章不会教大家如何实现一个具体的动画效果,我会从动画的本质出发,来说说 iOS 动画的原理与实现方式. 什么是动画 动画,顾名思义,就是能“动”的画.人的眼睛对图像有短暂的记忆效应,所以当眼睛看到 ...