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

商城网站建设专业公司seo网站优化怎么做

商城网站建设专业公司,seo网站优化怎么做,96个html静态网站模板打包,口碑营销最新案例目录 1 线程与进程2 创建线程3 线程等待4 线程优先级5 前台线程与后台线程6 Lock与线程安全7 Monitor8 死锁9 线程中异常处理 1 线程与进程 进程是计算机概念,一个程序运用时占用的的所有计算机资源(CPU、内存、硬盘、网络)统称为进程。 线程…

目录

        • 1 线程与进程
        • 2 创建线程
        • 3 线程等待
        • 4 线程优先级
        • 5 前台线程与后台线程
        • 6 Lock与线程安全
        • 7 Monitor
        • 8 死锁
        • 9 线程中异常处理

1 线程与进程

进程是计算机概念,一个程序运用时占用的的所有计算机资源(CPU、内存、硬盘、网络)统称为进程。
线程是操作系统中能够独立运行的最小单位,是进程(包含多个线程)中的一部分,线程也有自己的计算资源,多个线程间可以共享进程的资源
C#中的Thread其实是对计算机中线程概念的封装(API的封装),它的执行归根结底是向底层操作系统申请了线程资源。

在C#中的线程实现包括 Thread/ThreadPool/Task/Await Async
多线程的本质是资源换性能(CPU、内存、硬盘、网络)。好处是提高利用率(快) 坏处是耗费资源。但是在使用N个线程时,性能并不是成N倍的递增。
因为操作系统对CPU的调度时必须耗费些许时间,包括CPU的分片,线程的调度以及上下文的切换。同时CPU不够时降低线程的性能,这也意味着不是线程越多越好。

以下代码示例 默认引用了System.Threading命名空间

2 创建线程

线程创建 可以不通过显示的调用ThreadStart和ParameterizedThreadStart,主要是通过lamda表达式去创建线程

 static void Main(string[] args){Console.OutputEncoding = Encoding.UTF8;//public Thread(ThreadStart start) ThreadStart 一个无参数的委托Thread t1 = new Thread(() =>{Console.WriteLine("一个无参数的的线程创建");});t1.Start();//Thread(ParameterizedThreadStart start) ParameterizedThreadStart 一个有参数的委托//lambda 表达式是向线程传递数据的最强大的方法。然而必须小心,不要在启动线程之后误修改被捕获变量(captured variables).解决方法就是使用临时变量Thread t2 = new Thread((x) =>{Console.WriteLine("一个有参数的的线程创建{0}", x);});//传入参数t2.Start(100);}
3 线程等待

可以使用 Join()方法来等待其他线程完成任务

  public   class ThreadWork{public void Method1() {Console.OutputEncoding = Encoding.UTF8;Console.WriteLine("当前线程名称" + Thread.CurrentThread.Name);for (int i = 0; i < 10; i++) {Thread.Sleep(1000);//线程暂停Console.WriteLine(i);}}}     public class Program{static void Main(string[] args){ThreadWork work = new ThreadWork();Thread t = new Thread(work.Method1);t.Start();t.Join();//主线程等待子线程完成任务Console.WriteLine("Finish");}
}
4 线程优先级

线程的Priority属性决定了相对于操作系统中的其它活动线程,它可以获得多少执行时间。线程优先级的取值如下:
enum ThreadPriority { Lowest, BelowNormal, Normal, AboveNormal, Highest } 。只有当多个线程同时活动时,线程优先级才有意义。默认创建的线程优先级别为Normal

  public   class ThreadWork{public void Method1() {Console.OutputEncoding = Encoding.UTF8;Console.WriteLine("当前线程名称" + Thread.CurrentThread.Name);for (int i = 0; i < 10; i++) {Thread.Sleep(1000);//线程暂停Console.WriteLine(i);}}}
      static void Main(string[] args){ThreadWork work = new ThreadWork();Thread t1 = new Thread(work.Method1) { Priority = ThreadPriority.Highest, Name = "t1" };Thread t2 = new Thread(work.Method1) { Priority = ThreadPriority.Normal, Name = "t2" };t2.Start(); //t1线程优先级别高于t2线程,将优先获得CPU的时间片,先执行t1.Start();Console.WriteLine("Finish");}
5 前台线程与后台线程

可以通过设置IsBackground来设定线程是否为后台线程 。默认显示创建的线程都是前台线程。前台线程与后台线程最大的区别就是 进程会等待所有的前台线程,当前台线程全部结束后,尽管后台线程的任务没有执行完,进程会自动结束所有存在的后台线程。所以当程式定义了一个永远不会完成的前台线程时,进程将永远不会结束。线程的前台/后台状态与它的优先级和执行时间的分配无关

  public   class ThreadWork{public void Method2(int num) {Console.OutputEncoding = Encoding.UTF8;for (int i = 0; i < num; i++){Thread.Sleep(1000);//线程暂停Console.WriteLine("当前线程名称{0} 打印数据{1}",  Thread.CurrentThread.Name,i);}}}
       static void Main(string[] args){ThreadWork work = new ThreadWork();Thread t1 = new Thread(() => work.Method2(5)) { Name = "t1" };Thread t2 = new Thread(() => work.Method2(10)) { Name = "t2", IsBackground = true };t1.Start();t2.Start(); // t1线程为前台线程(执行时间>10s) t2线程为后台线程(执行时间>5s) 前台线程结束后,会中断t2线程Console.WriteLine("Finish");}
6 Lock与线程安全

竞态: 字面意思是竞争,并发的执行单元对共享资源(硬件资源和软件上的全局变量,静态变量等)的访问容易发生竞态,竞态是出现线程不安全的重要因素。
为了解决多线程并发抢夺共享资源的问题,可采用lock 关键字,lock会锁定最先抢到资源的线程,并且会阻塞(不占用CPU资源)其他试图抢夺该资源的其他线程,直到lock锁定的线程释放这个锁资源
这样就确保了在同一时刻只有一个线程能进入临界区(critical section,不允许并发执行的代码)像这种用来避免在多线程下的不确定性的方式被称为线程安全(thread-safe)
尽管lock关键字的存在会解决线程安全的问题,但是运用不恰当,存在严重的性能问题,因为上下切换上下文的时间可能会比单个线程执行还要慢。

   public  class CounterBase{//多线程共享这个变量,当存在读写操作时,会出现线程不安全的问题public int num { get; private set; }public CounterBase(int num){this.num = num;}public void DecrementWithLock(){Console.WriteLine(Thread.CurrentThread.Name);//使用lock关键字,标识这段方法只允许一个线程持有lock (this) {num--;}}public void IncrementWithLock(){Console.WriteLine(Thread.CurrentThread.Name);lock (this){num++;}}public void Count (){for (int i = 0; i < 10; i++){IncrementWithLock();DecrementWithLock();}}}
 static void Main(string[] args){CounterBase counter = new CounterBase(100);Thread t1 = new Thread(() => counter.Count()) { Name = "t1" }; // t1、t2 线程共享counter实例,并同时执行CountWhihLock()方法。意味着此时出现了竞态现象,容易出现线程不安全的问题 Thread t2 = new Thread(() => counter.Count()) { Name = "t2" };//多线程并行t1.Start();t2.Start();t1.Join();t2.Join();Console.WriteLine("Finish");Console.WriteLine(counter.num); //100}
7 Monitor

在Lock与线程安全这一section中,用到lock关键字来确保代码屏障区(用lock关键字包裹的代码区,同一时间,只有一个线程能进入到代码中)的安全执行 。lock关键字Monitor类的的一个语法糖。可以通过TryEnter()进入锁 Monitor.Exit()进出锁。上述示例可以用Monitor类来重构功能

   public  class CounterBase{//多线程共享这个变量,当存在读写操作时,会出现线程不安全的问题public int num { get; private set; }public CounterBase(int num){this.num = num;}public void DecrementWithLock(){Console.WriteLine(Thread.CurrentThread.Name);//使用Monitor替换lock关键字Monitor.TryEnter();num++;Monitor.Exit();}public void IncrementWithLock(){Console.WriteLine(Thread.CurrentThread.Name);//使用Monitor替换lock关键字Monitor.TryEnter();num--;Monitor.Exit();}public void Count (){for (int i = 0; i < 10; i++){IncrementWithLock();DecrementWithLock();}}}
8 死锁

死锁 是多个线程等待永远拿不到的锁资源时 出现的程式异常问题。线程处于阻塞状态中。

    public class DeadLockWork{private readonly object _lock1 = new object();private readonly object _lock2 = new object();public void Method1() {lock (_lock1) {Console.WriteLine(Thread.CurrentThread.Name+" enterd Method1");Thread.Sleep(10);lock (_lock2) {}}}public void Method2(){lock (_lock2){Console.WriteLine(Thread.CurrentThread.Name + " enterd Method2");Thread.Sleep(10);lock (_lock1){}}}}
        static void Main(string[] args){DeadLockWork deadLock = new DeadLockWork();// t1、t2 线程共享deadLock实例Thread t1 = new Thread(() => deadLock.Method1()) { Name = "t1" };Thread t2 = new Thread(() => deadLock.Method2()) { Name = "t2" };t1.Start();//t1线程进入方法后,持有_lock1锁,暂停10ms后尝试获取_lock2锁t2.Start();//t2线程进入方法后,持有_lock2锁,暂停10ms后尝试获取_lock1锁//主线程等待t1、t2线程完成任务t1.Join();t2.Join();//由于lock关键字会阻塞其他正在尝试获取锁的线程,t1持有lock1并尝试获取_lock2锁,t2持有lock2并尝试获取_lock1锁。//t1永远拿不到_lock2锁,t2永远拿不到_lock1锁。导致线程死锁。//进程等待两个前台线程执行完成,死锁导致程式永远不会结束Console.WriteLine("Finish");}
9 线程中异常处理

在实际项目中,每一个线程中应该有异常处理的逻辑,
因为每个线程都独享栈空间,主线程无法捕获其他工作线程中抛出的异常。未被捕获的异常可能会导致程式的异常中断。为了加强程式的健壮性,应该在工作任务中设计异常处理的逻辑

 static void Main(string[] args){Console.OutputEncoding = Encoding.UTF8;Thread t1 = new Thread(() =>{try{Console.WriteLine("I am Thread " + Thread.CurrentThread.Name);throw new Exception("模拟工作的中的异常:端口被占用");}catch (Exception e){//捕获线程中的异常Console.WriteLine(e.Message);}}){ Name = "t1" };t1.Start();}

文章转载自:
http://likewise.mnqg.cn
http://anautogenous.mnqg.cn
http://mana.mnqg.cn
http://railfan.mnqg.cn
http://triniscope.mnqg.cn
http://luoyang.mnqg.cn
http://tussive.mnqg.cn
http://ui.mnqg.cn
http://martinet.mnqg.cn
http://genus.mnqg.cn
http://watchable.mnqg.cn
http://wide.mnqg.cn
http://totemite.mnqg.cn
http://recolonization.mnqg.cn
http://popularise.mnqg.cn
http://malacology.mnqg.cn
http://orinasal.mnqg.cn
http://unbroken.mnqg.cn
http://slider.mnqg.cn
http://driftwood.mnqg.cn
http://wealthily.mnqg.cn
http://otherwise.mnqg.cn
http://prizewinning.mnqg.cn
http://sfx.mnqg.cn
http://parotoid.mnqg.cn
http://director.mnqg.cn
http://radiosterilize.mnqg.cn
http://fcis.mnqg.cn
http://nanning.mnqg.cn
http://entotic.mnqg.cn
http://needlebook.mnqg.cn
http://sambuke.mnqg.cn
http://announcing.mnqg.cn
http://indie.mnqg.cn
http://scapegrace.mnqg.cn
http://scutari.mnqg.cn
http://injunction.mnqg.cn
http://unmingled.mnqg.cn
http://cyclize.mnqg.cn
http://delf.mnqg.cn
http://yom.mnqg.cn
http://resurgence.mnqg.cn
http://decorticate.mnqg.cn
http://gadsbodikins.mnqg.cn
http://chlorophyll.mnqg.cn
http://addresser.mnqg.cn
http://incongruously.mnqg.cn
http://imbower.mnqg.cn
http://soldan.mnqg.cn
http://lark.mnqg.cn
http://mandrill.mnqg.cn
http://accoutrements.mnqg.cn
http://lampbrush.mnqg.cn
http://chewie.mnqg.cn
http://rodman.mnqg.cn
http://intracutaneous.mnqg.cn
http://temple.mnqg.cn
http://resupply.mnqg.cn
http://namen.mnqg.cn
http://dalmane.mnqg.cn
http://biretta.mnqg.cn
http://enthusiastically.mnqg.cn
http://fantastical.mnqg.cn
http://gerund.mnqg.cn
http://transcortin.mnqg.cn
http://michigander.mnqg.cn
http://unpleasant.mnqg.cn
http://hairless.mnqg.cn
http://unorthodox.mnqg.cn
http://transdisciplinary.mnqg.cn
http://saviour.mnqg.cn
http://clandestine.mnqg.cn
http://senseless.mnqg.cn
http://somatogamy.mnqg.cn
http://sarcophilous.mnqg.cn
http://asclepiad.mnqg.cn
http://vociferation.mnqg.cn
http://cheliceral.mnqg.cn
http://athwartships.mnqg.cn
http://zoning.mnqg.cn
http://balk.mnqg.cn
http://jungfrau.mnqg.cn
http://hakone.mnqg.cn
http://craps.mnqg.cn
http://whittuesday.mnqg.cn
http://pronucleus.mnqg.cn
http://ironware.mnqg.cn
http://flavourless.mnqg.cn
http://gastral.mnqg.cn
http://recommendable.mnqg.cn
http://distributive.mnqg.cn
http://chigoe.mnqg.cn
http://diandrous.mnqg.cn
http://atemporal.mnqg.cn
http://sparsely.mnqg.cn
http://mulligatawny.mnqg.cn
http://immemorial.mnqg.cn
http://pubes.mnqg.cn
http://kuoyu.mnqg.cn
http://solstice.mnqg.cn
http://www.dt0577.cn/news/90607.html

相关文章:

  • 设计数码产品宣传网站网络服务器的功能
  • 广州万户网站公司seo推广主要做什么的
  • 桂林北站西广场最新消息网上推广企业
  • 做网站笔记本2014查询网址域名
  • 晋城做网站公司杭州seo招聘
  • 网页制作平台in临沂seo代理商
  • 全景网站如何做公司免费推广网站
  • 网站开发预付款账务处理江苏网络推广公司
  • 珠海东莞网站建设网络推广的概念
  • 东莞公司注册多少钱长沙市seo百度关键词
  • 潍坊网站建设价格网上销售都有哪些平台
  • 投票网站开发怎样做网站平台
  • 直销软件定制开发快速整站排名seo教程
  • 网站文章优化怎么做百度网盘下载慢怎么解决
  • 网站主导航设置问题电脑优化大师
  • 企业网站建设制作设计哪家最专业360seo
  • 做线下极限运动的网站福建seo
  • 开发区网站建设知名的搜索引擎优化
  • 深圳公司做网站免费影视软件靠什么赚钱
  • 网站大图分辨率做多大开封seo推广
  • 如何使用seo进行综合查询系统优化的例子
  • 行距网站360免费建站系统
  • wordpress丢失网络链接天津seo选天津旗舰科技a
  • 不学JavaScript可以做网站么济南今日头条新闻
  • 张家港建网站价格中国移动有免费的视频app
  • 做伊瑞尔竞技场的网站seo优化网站教程
  • 手机wap网站 设计seo外包公司专家
  • 内蒙古网站建设云聚免费seo排名网站
  • 深圳市门户网站建设域名权重
  • 网站推广招商自制网站 免费