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());
                }
            }
        }
    }

  • 相关阅读:
    GlusterFS-分布式存储集群部署
    keepalived+HAproxy集群部署
    LB-HAproxy负载均衡部署
    Pacemaker高可用环境实践
    Nginx-负载均衡部署
    LB-LVS常见模式NAT/DR部署
    HTTPS原理、应用
    LDAP-autofs挂载用户验证
    GPG-非对称加密
    大数据入门学习(Linux)
  • 原文地址:https://www.cnblogs.com/duanlinlin/p/3142912.html
Copyright © 2011-2022 走看看