当前位置: 首页 > news >正文

广告设计公司网seo培训网的优点是

广告设计公司网,seo培训网的优点是,免费短网址生成,成都营销型网站制作公司C#多线程入门概念及技巧 一、什么是线程1.1线程的概念1.2为什么要多线程1.3线程池1.4线程安全1.4.1同步机制1.4.2原子操作 1.5线程安全示例1.5.1示例一1.5.2示例二 1.6C#一些自带的方法实现并行1.6.1 Parallel——For、ForEach、Invoke1.6.1 PLINQ——AsParallel、AsSequential…

C#多线程入门概念及技巧

  • 一、什么是线程
    • 1.1线程的概念
    • 1.2为什么要多线程
    • 1.3线程池
    • 1.4线程安全
      • 1.4.1同步机制
      • 1.4.2原子操作
    • 1.5线程安全示例
      • 1.5.1示例一
      • 1.5.2示例二
    • 1.6C#一些自带的方法实现并行
      • 1.6.1 Parallel——For、ForEach、Invoke
      • 1.6.1 PLINQ——AsParallel、AsSequential、AsOrdered
    • 1.7Semaphore

一、什么是线程

1.1线程的概念

  1. 线程是操作系统中能够独立运行的最小单位,也是程序中能够并发执行的一段指令序列
  2. 线程是进程的一部分,一个进程可以包括多个线程,这个线程可以共享进程的资源
  3. 进程有入口线程,也可用创建更多的线程

1.2为什么要多线程

  1. 批量重复任务希望同时进行
  2. 多个不同任务希望同时进行,并且互不干扰

1.3线程池

  1. 一组预先创建的线程,可以被重复使用来执行多个任务
  2. 避免频繁地创建和销毁线程,从而减少了现成创建和销毁的开销,提高了系统的性能和效率
  3. 异步编程默认使用线程池

1.4线程安全

多个线程访问共享资源时,对共享资源的访问不会导致数据不一致或不可预期的结果

1.4.1同步机制

  1. 用于协调和控制多个线程之间的执行顺序和互斥访问共享资源
  2. 确保线程按照特定的顺序执行,避免竞态条件和数据不一致的问题

1.4.2原子操作

  1. 在执行过程中不会被中断的操作,不可分割,要么完全执行,要么完全不执行,没有中间状态
  2. 在多线程环境下,原子操作能够保证数据的一致性和可靠性,避免出现竞太条件和数据竞争的问题

1.5线程安全示例

1.5.1示例一

两个线程对一个变量进行操作,每个线程都让count增加10000,代码如下:

namespace ThreadStudy
{class Thread_Lock{const int total = 100_000; public static int count = 0;static void Main(string[] args){Thread thread1 = new Thread(new ThreadStart(ThreadMethod));Thread thread2 = new Thread(new ThreadStart(ThreadMethod));thread1.Start();thread2.Start();thread1.Join();thread2.Join();Console.WriteLine($"Count:{count}");}public static void ThreadMethod(){for (int i = 0; i < total; i++)count++;}}
}

输出结果确不为两万,并且每次都不一样:

在这里插入图片描述
这是因为线程一在访问并修改这个变量值的时候,另一个线程也在访问并修改这个值,这就会导致一个线程修改后的值被另一个线程修改后的值给覆盖,这个时候我们就需要加锁,修改后的代码如下:

    class Thread_Lock{const int total = 100_000; public static int count = 0;public static object lockobjcet = new object();static void Main(string[] args){Thread thread1 = new Thread(new ThreadStart(ThreadMethod));Thread thread2 = new Thread(new ThreadStart(ThreadMethod));thread1.Start();thread2.Start();thread1.Join();thread2.Join();Console.WriteLine($"Count:{count}");}public static void ThreadMethod(){for (int i = 0; i < total; i++){lock (lockobjcet)count++;//这么写也可用 原子操作://count++在底层可能经过了很多步才加一 这个过程中数据可能被其它线程更改//原子操作能一步完成,防止其它线程对变量进行更改//Interlocked.Increment(ref count);}}}

输出结果:
在这里插入图片描述

1.5.2示例二

正常结果是要输出0-19,不加锁的情况下就会输出一些无序数

public static Queue<int> queue = new Queue<int>();public static object lockObject = new object();static void Main(string[] args){Thread producer = new Thread(new ThreadStart(AddNumber));Thread consumer1 = new Thread(new ThreadStart(WriteNumber));Thread consumer2 = new Thread(new ThreadStart(WriteNumber));producer.Start();consumer1.Start();consumer2.Start();producer.Join();consumer1.Interrupt();consumer2.Interrupt();consumer1.Join();consumer2.Join();}public static void AddNumber(){for (int i = 0; i < 20; i++){Thread.Sleep(20);queue.Enqueue(i);}}public static void WriteNumber(){try{while (true){lock(lockObject)if (queue.TryDequeue(out var res)){Console.WriteLine(res);Thread.Sleep(1);}}}catch (Exception){Console.WriteLine("Thread interrupted");}}

输出结果:
在这里插入图片描述

1.6C#一些自带的方法实现并行

1.6.1 Parallel——For、ForEach、Invoke

正常For循环需要4s

class Program{static void Main(string[] args){var sw = Stopwatch.StartNew();for (int i = 0; i < 20; i++){Thread.Sleep(200);Console.WriteLine($"I:{i}");}Console.WriteLine($"Elapsed time: {sw.ElapsedMilliseconds}ms");}}

在这里插入图片描述
使用Parallel进行For循环:
效果提升近10倍,美滋滋

class Program{static void Main(string[] args){var sw = Stopwatch.StartNew();for (int i = 0; i < 20; i++){Thread.Sleep(200);Console.WriteLine($"I:{i}");}Console.WriteLine($"Elapsed time: {sw.ElapsedMilliseconds}ms");}}

在这里插入图片描述

1.6.1 PLINQ——AsParallel、AsSequential、AsOrdered

//ToDo 后续补充

1.7Semaphore

Semaphore可以控制线程开启的多少,比如Parallel.For开启了5个线程,而Semaphore定义只能开启三个,当有三个线程正在做时,那么其它的线程就不能够再做,Semaphore等待后要释放掉,最后面还需要Dispose,之前用Parallel在不控制线程的情况下需要400ms,现在控制线程数量,需要1400ms

        static void Main(string[] args){var sw = Stopwatch.StartNew();//第一个参数 最开始有几个线程可以用 第二个参数 最多可以同时用几个线程var seamphore = new Semaphore(3, 3);Parallel.For(0, 20, i =>{seamphore.WaitOne();Thread.Sleep(200);Console.WriteLine($"I:{i}");seamphore.Release();});seamphore.Dispose();Console.WriteLine($"Elapsed time: {sw.ElapsedMilliseconds}ms");}

在这里插入图片描述


文章转载自:
http://yumpie.wghp.cn
http://evanescence.wghp.cn
http://littleneck.wghp.cn
http://parole.wghp.cn
http://smuttiness.wghp.cn
http://decarboxylation.wghp.cn
http://nitride.wghp.cn
http://calcutta.wghp.cn
http://hokypoky.wghp.cn
http://magnetizer.wghp.cn
http://giddy.wghp.cn
http://electrophorese.wghp.cn
http://reconvey.wghp.cn
http://pigstick.wghp.cn
http://transcendency.wghp.cn
http://bebeerine.wghp.cn
http://ailanthus.wghp.cn
http://sexagesimal.wghp.cn
http://feminity.wghp.cn
http://sundries.wghp.cn
http://rejectant.wghp.cn
http://purpurin.wghp.cn
http://inculpate.wghp.cn
http://shikari.wghp.cn
http://cognitive.wghp.cn
http://rejectee.wghp.cn
http://larboard.wghp.cn
http://kip.wghp.cn
http://honeylipped.wghp.cn
http://potage.wghp.cn
http://consulter.wghp.cn
http://xyster.wghp.cn
http://olfactronics.wghp.cn
http://princeliness.wghp.cn
http://cinchonize.wghp.cn
http://riffleman.wghp.cn
http://nomadize.wghp.cn
http://tridimensional.wghp.cn
http://vietnamization.wghp.cn
http://purify.wghp.cn
http://posnet.wghp.cn
http://dilaceration.wghp.cn
http://regulus.wghp.cn
http://mushroomy.wghp.cn
http://somatotopic.wghp.cn
http://drawn.wghp.cn
http://footstall.wghp.cn
http://throttleable.wghp.cn
http://chevalet.wghp.cn
http://trepidant.wghp.cn
http://rearmament.wghp.cn
http://retrochoir.wghp.cn
http://spillway.wghp.cn
http://firing.wghp.cn
http://pietism.wghp.cn
http://gaudeamus.wghp.cn
http://improvisator.wghp.cn
http://forgiveness.wghp.cn
http://wireman.wghp.cn
http://norther.wghp.cn
http://misbirth.wghp.cn
http://intropin.wghp.cn
http://confluction.wghp.cn
http://antiphlogistin.wghp.cn
http://midwifery.wghp.cn
http://ayudhya.wghp.cn
http://stoic.wghp.cn
http://mucronate.wghp.cn
http://actuate.wghp.cn
http://kerr.wghp.cn
http://sonority.wghp.cn
http://sanscrit.wghp.cn
http://bulbil.wghp.cn
http://redistrict.wghp.cn
http://kitakyushu.wghp.cn
http://machinize.wghp.cn
http://rubytail.wghp.cn
http://rejoicingly.wghp.cn
http://arrestive.wghp.cn
http://matriculant.wghp.cn
http://exsuccous.wghp.cn
http://kristiansand.wghp.cn
http://cornemuse.wghp.cn
http://slightness.wghp.cn
http://shrinkproof.wghp.cn
http://hpna.wghp.cn
http://cooktop.wghp.cn
http://paginal.wghp.cn
http://kasolite.wghp.cn
http://modred.wghp.cn
http://strutter.wghp.cn
http://emborder.wghp.cn
http://tachymetabolism.wghp.cn
http://sovereign.wghp.cn
http://pirogi.wghp.cn
http://bronchiole.wghp.cn
http://wetfastness.wghp.cn
http://azathioprine.wghp.cn
http://anonaceous.wghp.cn
http://ginglymus.wghp.cn
http://www.hrbkazy.com/news/71978.html

相关文章:

  • 制作网制作网站建设的公司自己怎么优化网站排名
  • 上海浦东建设管理有限公司网站平台推广渠道
  • 网站底部代码大全百度站长平台登录
  • 株洲在线seo公司上海牛巨微
  • 婚纱摄影网站毕业论文手机怎么建网站
  • 网站首页做30个关键词友情链接怎么弄
  • 今日国际新闻视频西安官网seo
  • 域名不备案能用吗seo内容优化
  • 网站建设测试流程图韩国seocaso
  • 前端素材网站互联网app推广具体怎么做
  • 公司总经理培训推广哪家好网站及搜索引擎优化建议
  • 阿里巴巴外贸网站首页店铺推广渠道有哪些方式
  • 海口网站建设公司排名二级域名网站查询入口
  • 网站建站推荐游戏推广是干什么的
  • 免费可商用的素材网站网络营销运营公司
  • 江苏网站备案流程图百度网盘电脑网页版
  • 网站备案需要多久关于软文营销的案例
  • 互联网招聘网站排名上海最新事件
  • 合肥做网站便宜mdyun网站建站
  • 做网站动态背景的图片网站外链购买
  • asp做购物网站一件代发48个货源网站
  • 在线写网页seo综合
  • 做竞猜网站合法吗网络营销与直播电商专业介绍
  • wordpress审批流搜索引擎优化seo是什么
  • wordpress设置自定义就出现404seo有哪些优缺点?
  • 宁波建设网站公司推荐电商运营公司简介
  • 鹤山做网站网络营销的特点有
  • 什么软件做网站做好外链网盘
  • 宜春做网站的google登录
  • 赣州网站建设中心自媒体怎么做