zoukankan      html  css  js  c++  java
  • 单线程与多线程

    多线程的目的是是提高速度,而是同时可以执行多个功能,我觉得速度相对会变慢一些,如下小案例:

    输出数字与字符串,用控制台应用程序实验:

    单线程:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;


    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //单线程
                Stopwatch watch = new Stopwatch();
                watch.Start();
                PrintNumb();
                PrintStr();
                watch.Stop();
                Console.WriteLine(watch.Elapsed.TotalMilliseconds);
                Console.ReadKey();
            }


            private static void PrintNumb()
            {
                for (int i = 0; i < 1000; i++)
                {
                    Console.WriteLine(i);
                }
            }


            private static void PrintStr()
            {
                for (int i = 0; i < 1000; i++)
                {
                    Console.WriteLine("您输出的是:"+i.ToString());
                }
            }
        }
    }

    多线程:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;


    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //双线程
                ThreadStart sprintnum = new ThreadStart(PrintNumb);
                Thread tnum = new Thread(sprintnum);
                ThreadStart tprintstr = new ThreadStart(PrintStr);
                Thread tntstr = new Thread(tprintstr);


                Stopwatch watch = new Stopwatch();
                watch.Start();
                tnum.Start();
                tntstr.Start();
                while (true)
                {
                    if (tnum.ThreadState==System.Threading.ThreadState.Stopped&&tntstr.ThreadState==System.Threading.ThreadState.Stopped)
                    {
                        watch.Stop();
                        Console.WriteLine(watch.Elapsed.TotalMilliseconds);
                        break;
                    }
                }
                Console.ReadKey();
            }


            private static void PrintNumb()
            {
                for (int i = 0; i < 1000; i++)
                {
                    Console.WriteLine(i);
                }
            }


            private static void PrintStr()
            {
                for (int i = 0; i < 1000; i++)
                {
                    Console.WriteLine("您输出的是:"+i.ToString());
                }
            }
        }
    }

  • 相关阅读:
    每一次面试都是提升(一)
    Gof设计模式分组
    MSSqlServer 通过sql语句 还原数据库(已有备份文件)
    自定义配置节与配置节的读取
    Javascript判断时间大小的方法
    C#微信开发之旅(十三):V2订单查询&退款(完结)
    C#微信开发之旅(十二):V2告警接口&维权接口
    C#微信开发之旅(十一):V2发货接口
    C#微信开发之旅(十):APP预支付及支付参数生成(V2)
    C#微信开发之旅(九):JSAPI支付(V3)
  • 原文地址:https://www.cnblogs.com/duanlinlin/p/3142912.html
Copyright © 2011-2022 走看看