国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

設計模式-命令

2019-11-06 06:19:15
字體:
來源:轉載
供稿:網友
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using 設計模式講解.Command;namespace 設計模式講解{    class PRogram    {        static void Main(string[] args)        {            /*命令模式,場景使用不同的方式實現多線程編程             命令:用戶發送不同的命令,接收者執行對應的方法             接收者:負責接收命令并且執行命令             調用者:負責調用命令            */            List<int> caculateList = new List<int>();            for (int i = 0; i < 10; i++)            {                caculateList.Add(i);            }            MTReceiver _MTReceiver = new MTReceiver();            ThreadPoolCmd _ThreadPoolCmd = new ThreadPoolCmd(_MTReceiver);            ParallelCmd _ParallelCmd = new ParallelCmd(_MTReceiver);            TaskCmd _TaskCmd = new TaskCmd(_MTReceiver);            Invoker _Invoker = new Invoker();            _Invoker.SetCommand(_ParallelCmd);            _Invoker.ExcuteCommand(caculateList);            Console.ReadKey();        }    }}

MTReceiver

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace 設計模式講解.Command{    public class MTReceiver    {        /// <summary>        /// 線程池        /// </summary>        /// <param name="caculateList"></param>        public void ThreadPoolMethod(List<int> caculateList)        {            foreach (var item in caculateList)            {                ThreadPool.QueueUserWorkItem(new WaitCallback(t =>                {                    Console.WriteLine("線程池計算:{0},線程ID:{1}", t, Thread.CurrentThread.ManagedThreadId);                }),item);            }        }        /// <summary>        /// 并行        /// </summary>        /// <param name="caculateList"></param>        public void ParallelMethod(List<int> caculateList)        {            Parallel.ForEach(caculateList, t =>            {                Console.WriteLine("線程池計算:{0},線程ID:{1}", t, Thread.CurrentThread.ManagedThreadId);            });        }        /// <summary>        /// 直接創建Task        /// </summary>        /// <param name="caculateList"></param>        public void TaskMethod(List<int> caculateList)        {            Task[] tasks = new Task[caculateList.Count];            for (int i = 0; i < tasks.Length; i++)            {                tasks[i] = Task.Factory.StartNew(() => {                    Console.WriteLine("線程池計算:{0},線程ID:{1}", i, Thread.CurrentThread.ManagedThreadId);                });            }                      Task.WaitAll(tasks);        }    }}

ICmd

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 設計模式講解.Command{    public abstract class ICmd    {        protected MTReceiver _MTReceiver;        public ICmd(MTReceiver _MTReceiver)        {            this._MTReceiver = _MTReceiver;        }        public abstract void Excute(List<int> caculateList);    }}

ThreadPoolCmd

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 設計模式講解.Command{    public class ThreadPoolCmd : ICmd    {        public ThreadPoolCmd(MTReceiver _MTReceiver) : base(_MTReceiver)        {        }        public override void Excute(List<int> caculateList)        {            this._MTReceiver.ThreadPoolMethod(caculateList);        }    }}

ParallelCmd

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 設計模式講解.Command{    public class ParallelCmd : ICmd    {        public ParallelCmd(MTReceiver _MTReceiver) : base(_MTReceiver)        {        }        public override void Excute(List<int> caculateList)        {            this._MTReceiver.ParallelMethod(caculateList);        }    }}

TaskCmd

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 設計模式講解.Command{    public class TaskCmd : ICmd    {        public TaskCmd(MTReceiver _MTReceiver) : base(_MTReceiver)        {        }        public override void Excute(List<int> caculateList)        {            this._MTReceiver.TaskMethod(caculateList);        }    }}

Invoker

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 設計模式講解.Command{    public class Invoker    {        private ICmd cmd;        public void SetCommand(ICmd cmd)        {            this.cmd = cmd;        }        public void ExcuteCommand(List<int> caculateList)        {            cmd.Excute(caculateList);        }    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昌乐县| 泰兴市| 马尔康县| 丹凤县| 秀山| 海口市| 延吉市| 洪湖市| 涟源市| 昭觉县| 双柏县| 南充市| 沅江市| 峡江县| 义乌市| 招远市| 永修县| 同心县| 眉山市| 柏乡县| 桑植县| 桑日县| 瓦房店市| 盐源县| 屏东市| 荣昌县| 龙陵县| 岳普湖县| 黄浦区| 东丰县| 北宁市| 开封市| 来安县| 洛浦县| 三江| 延吉市| 中山市| 屯门区| 鹤岗市| 廉江市| 济南市|