110,534
社区成员
发帖
与我相关
我的任务
分享
Parallel.For(0, 2, i => {
switch(i)
{
case 0://艺龙
break;
case 1://携程
break;
}
//将获取结果添加到result
});
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Tasks
{
/// <summary>
/// c#多线程并行demo
/// </summary>
class Program
{
private static BlockingCollection<System.Collections.Generic.KeyValuePair<string, Hotel>> hotels = new BlockingCollection<System.Collections.Generic.KeyValuePair<string, Hotel>>();
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
Parallel.For(0, 10, (x) => {
string queryId = x.ToString();
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>> lstHotels
= QueryHotels(new QueryHotelParameter(), queryId);
foreach (System.Collections.Generic.KeyValuePair<string, Hotel> item in lstHotels)
{
hotels.Add(item);
}
});
foreach (var hotel in hotels)
{
Console.WriteLine(string.Format("第{0}次查询,查询结果为{1}",hotel.Key,hotel.Value.Name));
}
Console.WriteLine("并行耗时:" + stopwatch.Elapsed.TotalSeconds);
Console.ReadLine();
}
private static System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>> QueryHotels(QueryHotelParameter parameter,string QweryId)
{
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>> lstHotels = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>>();
Parallel.Invoke(
() => lstHotels.Add(QueryQuNarHotels(parameter, QweryId)),
() => lstHotels.Add(QueryElongHotels(parameter, QweryId))
);
Task.WaitAll();
return lstHotels;
}
/// <summary>
/// 获取去哪酒店
/// </summary>
/// <param name="parameter"></param>
private static System.Collections.Generic.KeyValuePair<string, Hotel> QueryQuNarHotels(QueryHotelParameter parameter, string QueryId)
{
System.Collections.Generic.KeyValuePair<string, Hotel> result = new System.Collections.Generic.KeyValuePair<string, Hotel>(QueryId, new Hotel { Name = "去哪的酒店", Price = 100 });
Thread.Sleep(1000);
return result;
}
/// <summary>
/// 获取艺龙酒店
/// </summary>
/// <param name="parameter"></param>
private static System.Collections.Generic.KeyValuePair<string, Hotel> QueryElongHotels(QueryHotelParameter parameter, string QueryId)
{
System.Collections.Generic.KeyValuePair<string, Hotel> result = new System.Collections.Generic.KeyValuePair<string, Hotel>(QueryId, new Hotel { Name = "艺龙的酒店", Price = 100 });
Thread.Sleep(1000);
return result;
}
public class Hotel
{
public string Name { set; get; }
public decimal Price { set; get; }
}
public class QueryHotelParameter
{
public string CityCode { set; get; }
public string ArrivalDate { set; get; }
public string DepartureDate { set; get; }
}
}
}
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Tasks
{
/// <summary>
/// c#多线程并行demo
/// </summary>
class Program
{
private static BlockingCollection<System.Collections.Generic.KeyValuePair<string, Hotel>> hotels = new BlockingCollection<System.Collections.Generic.KeyValuePair<string, Hotel>>();
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
Parallel.For(0, 10, (x) => {
string queryId = x.ToString();
QueryHotels(null, queryId);
});
foreach (var hotel in hotels)
{
Console.WriteLine(string.Format("第{0}次查询,查询结果为{1}",hotel.Key,hotel.Value.Name));
}
Console.WriteLine("并行耗时:" + stopwatch.Elapsed.TotalSeconds);
Console.ReadLine();
}
private static void QueryHotels(QueryHotelParameter parameter,string QweryId)
{
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>> lstHotels = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>>();
Parallel.Invoke(
() => lstHotels.Add(QueryQuNarHotels(parameter, QweryId)),
() => lstHotels.Add(QueryElongHotels(parameter, QweryId))
);
Task.WaitAll();
foreach (System.Collections.Generic.KeyValuePair<string, Hotel> item in lstHotels)
{
hotels.Add(item);
}
}
/// <summary>
/// 获取去哪酒店
/// </summary>
/// <param name="parameter"></param>
private static System.Collections.Generic.KeyValuePair<string, Hotel> QueryQuNarHotels(QueryHotelParameter parameter, string QueryId)
{
System.Collections.Generic.KeyValuePair<string, Hotel> result = new System.Collections.Generic.KeyValuePair<string, Hotel>(QueryId, new Hotel { Name = "去哪的酒店", Price = 100 });
Thread.Sleep(1000);
return result;
}
/// <summary>
/// 获取艺龙酒店
/// </summary>
/// <param name="parameter"></param>
private static System.Collections.Generic.KeyValuePair<string, Hotel> QueryElongHotels(QueryHotelParameter parameter, string QueryId)
{
System.Collections.Generic.KeyValuePair<string, Hotel> result = new System.Collections.Generic.KeyValuePair<string, Hotel>(QueryId, new Hotel { Name = "艺龙的酒店", Price = 100 });
Thread.Sleep(1000);
return result;
}
public class Hotel
{
public string Name { set; get; }
public decimal Price { set; get; }
}
public class QueryHotelParameter
{
public string CityCode { set; get; }
public string ArrivalDate { set; get; }
public string DepartureDate { set; get; }
}
}
}
Parallel.For(0, 10, (x) => {
string queryId = x.ToString();
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>> lstHotels
= QueryHotels(new QueryHotelParameter(), queryId);
foreach (System.Collections.Generic.KeyValuePair<string, Hotel> item in lstHotels)
{
hotels.Add(item);
}
});
string queryId = "实际id";
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>> lstHotels
= QueryHotels(new QueryHotelParameter(), queryId);
foreach (System.Collections.Generic.KeyValuePair<string, Hotel> item in lstHotels)
{
//实际处理或插入静态类
}
class Program
{
static void Main(string[] args)
{
Task<int>[] tasks=new Task<int>[]{add(2),add(1),add(3)};
var sum = tasks.AsParallel().Sum(p => p.Result); //我们可以直接plinq
var lst= tasks.AsParallel().OrderBy(p => p.Result);
foreach (Task<int> task in lst)
{
Console.WriteLine($"排序{task.Result}");
}
Console.WriteLine($"累加计算结果:{sum}");
Console.ReadKey();
}
private static Random rand = new Random(DateTime.Now.Millisecond);
static async Task<int> add(int a)
{
//我来模拟你异步耗时访问,随机消耗点时间
await Task.Delay(TimeSpan.FromSeconds(rand.Next(0, 10)));
Console.WriteLine($"生成顺序{a}");
return a;
}
Parallel.For(0, 10, (x) => {
string queryId = x.ToString();
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>> lstHotels
= QueryHotels(new QueryHotelParameter(), queryId);
foreach (System.Collections.Generic.KeyValuePair<string, Hotel> item in lstHotels)
{
hotels.Add(item);
}
});
string queryId = "实际id";
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, Hotel>> lstHotels
= QueryHotels(new QueryHotelParameter(), queryId);
foreach (System.Collections.Generic.KeyValuePair<string, Hotel> item in lstHotels)
{
//实际处理或插入静态类
}
var result = new List<Hotel>();
Parallel.For(0, 2, i => {
switch(i)
{
case 0://艺龙
result.AddRange(QueryElongHotels(parameter));
break;
case 1://携程
result.AddRange(QueryQuNarHotels(parameter));
break;
}
});
return result;