1.普通示例
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace PLinq
{
class Program
{
static IList<Person> _persons = new List<Person>();
static void Main(string[] args)
{
try
{
CreateTextDB();
PLINQ_Where();
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Exception Message:{0}", ex.Message.Trim()));
}
finally
{
Console.ReadLine();
}
}
/// <summary>
/// 筛选
/// </summary>
private static void PLINQ_Where()
{
Stopwatch _wacth = new Stopwatch();
_wacth.Start();
IList<Person> _personWhereNormal = _persons.Where(p => p.Age > 10 && p.Age < 50).ToList();
_wacth.Stop();
Console.WriteLine(string.Format("Normal LINQ Where Cost Time:{0}", _wacth.ElapsedMilliseconds));
_wacth.Restart();
//WithDegreeOfParallelism
IList<Person> _personWhereParallel = _persons.AsParallel<Person>().Where(p => p.Age > 10 && p.Age < 50).ToList();
_wacth.Stop();
Console.WriteLine(string.Format("PLINQ Where Cost Time:{0}", _wacth.ElapsedMilliseconds));
}
/// <summary>
/// 创建测试数据
/// </summary>
private static void CreateTextDB()
{
Stopwatch _wacth = new Stopwatch();
_wacth.Start();
Parallel.For(0, 10000000, (i, loopstatus) =>
{
Person _person = new Person();
_person.FristName = "Yan";
_person.LastName = string.Format("Zhiwei {0}", i);
_person.RegisterTime = DateTime.Now;
_person.Age = i;
lock (((ICollection)_persons).SyncRoot)
{
_persons.Add(_person);
}
});
_wacth.Stop();
Console.WriteLine(string.Format("Create TextDB Cost Time:{0},Count:{1}", _wacth.ElapsedMilliseconds, _persons.Count));
}
}
class Person
{
public string FristName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public DateTime RegisterTime { get; set; }
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }代码效果
2.异常处理
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; namespace PLinq { class Program { static IList<Person> _persons = new List<Person>(); static void Main(string[] args) { try { CreateTextDB(); PINQ_Exception(); } catch (Exception ex) { Console.WriteLine(string.Format("Exception Message:{0}", ex.Message.Trim())); } finally { Console.ReadLine(); } } private static void PINQ_Exception() { /* * 这样处理,当出现异常的时候不会影响下次foralll遍历 */ Func<int, bool> isTure = (age) => { try { if (age > 1989222) throw new Exception("PLINQ Text"); Console.WriteLine(age); return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } }; _persons.AsParallel<Person>().ForAll(p => isTure(p.Age)); } /// <summary> /// 创建测试数据 /// </summary> private static void CreateTextDB() { Stopwatch _wacth = new Stopwatch(); _wacth.Start(); Parallel.For(0, 10000000, (i, loopstatus) => { Person _person = new Person(); _person.FristName = "Yan"; _person.LastName = string.Format("Zhiwei {0}", i); _person.RegisterTime = DateTime.Now; _person.Age = i; lock (((ICollection)_persons).SyncRoot) { _persons.Add(_person); } }); _wacth.Stop(); Console.WriteLine(string.Format("Create TextDB Cost Time:{0},Count:{1}", _wacth.ElapsedMilliseconds, _persons.Count)); } } class Person { public string FristName { get; set; } public string LastName { get; set; } public int Age { get; set; } public DateTime RegisterTime { get; set; } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
代码效果
3.取消 PLINQ 查询
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace PLinq { class Program { static IList<Person> _persons = new List<Person>(); static void Main(string[] args) { try { CreateTextDB(); PINQ_Cancellation(); } catch (Exception ex) { Console.WriteLine(string.Format("Exception Message:{0}", ex.Message.Trim())); } finally { Console.ReadLine(); } } private static void PINQ_Cancellation() { try { /* * 当您在用户代码中处理取消时,不必在查询定义中使用 WithCancellation<TSource>。 但是,我们建议您这样做,原因是 WithCancellation<TSource> 对查询性能没有影响, * 并且它使取消能够由查询运算符和用户代码进行处理。 为了确保系统响应能力,我们建议您大约每毫秒检查是否存在取消一次; * 不过,任何 10 毫秒以下的期间都被视为可接受。 此频率对代码的性能应没有负面影响。 ——MSDN */ CancellationTokenSource _cancel = new CancellationTokenSource(); Func<int, bool> isTure = (age) => { if (age > 1989222) _cancel.Cancel(); Console.WriteLine(age); return true; }; _persons.AsParallel<Person>().WithCancellation(_cancel.Token).ForAll(p => isTure(p.Age)); } catch (OperationCanceledException ex) { Console.WriteLine(string.Format("OperationCanceledException Message:{0}", ex.Message)); } } /// <summary> /// 创建测试数据 /// </summary> private static void CreateTextDB() { Stopwatch _wacth = new Stopwatch(); _wacth.Start(); Parallel.For(0, 10000000, (i, loopstatus) => { Person _person = new Person(); _person.FristName = "Yan"; _person.LastName = string.Format("Zhiwei {0}", i); _person.RegisterTime = DateTime.Now; _person.Age = i; lock (((ICollection)_persons).SyncRoot) { _persons.Add(_person); } }); _wacth.Stop(); Console.WriteLine(string.Format("Create TextDB Cost Time:{0},Count:{1}", _wacth.ElapsedMilliseconds, _persons.Count)); } } class Person { public string FristName { get; set; } public string LastName { get; set; } public int Age { get; set; } public DateTime RegisterTime { get; set; } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
代码效果