zoukankan      html  css  js  c++  java
  • 并行

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace MoreThread
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                TestParallelFor t1 = new TestParallelFor();
                t1.DoSomeThing();
                TestParallelForEach t2 = new TestParallelForEach();
                t2.DoSomeThing();
                Console.ReadKey();
            }
        }
    
        public interface ITestThread
        {
            void DoSomeThing();
        }
    
        public class TestParallelFor : ITestThread
        {
            private int GetData(int i)
            {
                Thread.Sleep(1000);
                return i;
            }
    
            public void DoSomeThing()
            {
                DateTime dt = DateTime.Now;
                Console.WriteLine("开始执行时间:" + DateTime.Now.ToString());
                List<int> list = new List<int>();
                System.Threading.Tasks.Parallel.For(0, 10, (i) =>
                {
                    list.Add(GetData(i));
                });
                Console.WriteLine("TestParallelFor-" + (DateTime.Now - dt).TotalMilliseconds.ToString() + "毫秒");
                Console.WriteLine("结束执行时间:" + DateTime.Now.ToString());
                list.ForEach(x =>
                {
                    Console.WriteLine(x);
                });
            }
        }
    
        public class TestParallelForEach : ITestThread
        {
            private int GetData(int i)
            {
                Console.WriteLine("I=" + i.ToString());
                Thread.Sleep(1000);
                return i;
            }
    
            public void DoSomeThing()
            {
                DateTime dt = DateTime.Now;
                Console.WriteLine("开始执行时间:" + DateTime.Now.ToString());
                List<int> list = new List<int>();
                List<int> list2 = new List<int>();
                for (int i = 0; i < 10; i++)
                {
                    list2.Add(i);
                }
                System.Threading.Tasks.Parallel.ForEach(list2, (i) =>
                {
                    list.Add(GetData(i));
                });
                list.ForEach(x =>
                {
                    Console.WriteLine(x);
                });
                Console.WriteLine("TestParallelForEach-" + (DateTime.Now - dt).TotalMilliseconds.ToString() + "毫秒");
                Console.WriteLine("结束执行时间:" + DateTime.Now.ToString());
            }
        }
    }

     
  • 相关阅读:
    vue 进行ajax请求,使用axios
    webpack 小记
    js闭包
    git小记
    sublime text3 插件安装
    mysql安装
    四十九、django单表操作,多表操作,一对多,多对多,分组查询,聚合查询,F、Q查询,自定义char字段,事务,only与defer ,choices字段
    四十八、模板层,标签,过滤器,继承和导入
    四十七、django路由匹配,分组,反向解析,路由分发,视图层
    四十六、django配置及增删改查
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2700425.html
Copyright © 2011-2022 走看看