任务并行案例

 并行简单案例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Parallel_Test
{
    [Serializable]
    public class Person
    {
        public int ID
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            ThreadPool.SetMaxThreads(1000, 1000);
            #region non Parallel
            //for (int n = 0; n < 3; n++)
            //{
            //    Thread.Sleep(2000);
            //    ThreadPoolMessage(GetPersonList()[n]);
            //}
            #endregion
            #region Parallel
            //Parallel.For(0, 3, n =>
            //  {
            //      Thread.Sleep(2000);
            //      ThreadPoolMessage(GetPersonList()[n]);
            //  });
            #endregion
            #region paralleStop
            Parallel.ForEach(GetPersonList(), (person, state) =>
             {
                 if (person.ID == 2)
                     state.Stop();
                 ThreadPoolMessage(person);
             });
            Console.ReadKey();
            #endregion
            Console.ReadKey();
        }
        static IList<Person> GetPersonList()
        {
            var personList = new List<Person>();
            var person1 = new Person();
            person1.ID = 1;
            person1.Name = "Leslie";
            person1.Age=30;
            personList.Add(person1);
            var person2 = new Person();
            person2.ID = 2;
            person2.Name = "Leslie";
            person2.Age = 30;
            personList.Add(person2);
            var person3 = new Person();
            person3.ID = 3;
            person3.Name = "Leslie";
            person3.Age = 30;
            personList.Add(person3);
            return personList;
        }
        static void ThreadPoolMessage(Person person)
        {
            int a, b;
            ThreadPool.GetAvailableThreads(out a, out b);
            string message = string.Format("Person ID:{0} Name:{1} Age:{2}\n" +
                " CurrentThreadId is {3}\n WorkerThreads is:{4}" +
                " CompletionPortThreads is:{5}\n", person.ID, person.Name, person.Age,
                Thread.CurrentThread.ManagedThreadId, a, b);
            Console.WriteLine(message);
        }
    }
}

 

 并行调用本地变量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelLocalVar
{
    public class Order
    {
        public int ID;
        public float Price;
    }
    public class OrderItem
    {
        public int ID;
        public string Goods;
        public int OrderID;
        public float Price;
        public int Count;
    }

    class Program
    {
        static void Main(string[] args)
        {
            ThreadPool.SetMaxThreads(1000, 1000);
            float totalPrice = 0f;
            var parallelResult = Parallel.ForEach(GetOrderList(),//数据集
                () => 0f,//Func委托,本地变量初始化
                (order, state, orderPrice) =>//托Func<Of T1,T2,T3,TResult>(数据,ParallelLoopState对象,本地变量)
                {
                    orderPrice = GetOrderItems().Where(item => item.OrderID == order.ID)
                    .Sum(item => item.Price * item.Count);
                    order.Price = orderPrice;
                    ThreadPoolMessage(order);
                    return orderPrice;
                },
                (finallyPrice) =>//Action委托,用于对每个线程的最终状态进行最终操作。
                {
                    totalPrice += finallyPrice;
                }
                );
            while (!parallelResult.IsCompleted)
                Console.WriteLine("Doing Work!");
            Console.WriteLine("Total Price is: " + totalPrice);
            Console.ReadKey();
        }
        static IList<Order> GetOrderList()
        {
            IList<Order> orderList = new List<Order>();
            Order order1 = new Order();
            order1.ID = 1;
            orderList.Add(order1);
            Order order2 = new Order();
            order2.ID = 2;
            orderList.Add(order2);
            Order order3 = new Order();
            order3.ID = 3;
            orderList.Add(order3);

            return orderList;
        }
        static IList<OrderItem> GetOrderItems()
        {
            IList<OrderItem> itemList = new List<OrderItem>();
            OrderItem orderItem1 = new OrderItem();
            orderItem1.ID = 1;
            orderItem1.Goods = "iPhone 4S";
            orderItem1.Price = 6700;
            orderItem1.Count = 2;
            orderItem1.OrderID = 1;
            itemList.Add(orderItem1);
            OrderItem orderItem2 = new OrderItem();
            orderItem2.ID = 2;
            orderItem2.Goods = "iPhone 4S";
            orderItem2.Price = 6700;
            orderItem2.Count = 2;
            orderItem2.OrderID = 1;
            itemList.Add(orderItem2);

            return itemList;
        }

        static void ThreadPoolMessage(Order order)
        {
            int a, b;
            ThreadPool.GetAvailableThreads(out a, out b);
            string message = string.Format("OrderID:{0}  OrderPrice:{1}\n" +
                    "  CurrentThreadId is {2}\n  WorkerThreads is:{3}" +
                   "  CompletionPortThreads is:{4}\n",
                    order.ID, order.Price,
                   Thread.CurrentThread.ManagedThreadId, a.ToString(), b.ToString());
            Console.WriteLine(message);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fwsylin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值