并行简单案例
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);
}
}
}