using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace 24
{
class Program
{
static void Main(string[] args)
{
//Action[] actions = new Action[] {
//new Action(()=>{
// Console.WriteLine("hello");
//}),
//new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//})
//};
//for (int i = 0; i < actions.Length; i++)
//{
// actions[i]();
//}
并行循环
//Parallel.Invoke(actions);
//List<int> list = new List<int>{ 1, 2, 3, 6, 8, 56, 78, 95, 23, 45, 65, 21, 34, 67, 87 };
//Stopwatch watch = new Stopwatch();
//watch.Start();
Parallel.For(1, 11, i => {
Console.WriteLine(i + ",线程ID:" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
});
//Parallel.ForEach<int>(list, n => {
// Console.WriteLine(n);
// Thread.Sleep(1000);
//});
//watch.Stop();
//Console.WriteLine("OK");
//Console.WriteLine(watch.Elapsed);
//Console.ReadKey();
// 产生测试资料
List<int> testData = new List<int>();
Random rand = new Random();
// 产生乱数列表
for (int i = 0; i < 1000000; i++)
{
testData.Add(rand.Next(1000));
//Console.WriteLine(rand.Next(10));
}
// 打印正确结果
Console.WriteLine(testData.Sum());
for (int i = 0; i < 5; i++)
{
Console.WriteLine();
TestFor(testData);
TestParallelFor(testData);
TestParallelForEach(testData);
}
Console.ReadKey();
}
static void TestParallelForEach(List<int> testData)
{
DateTime time1 = DateTime.Now;
ConcurrentStack<int> resultData = new ConcurrentStack<int>();
Parallel.ForEach(testData, (item, loppState) =>
{
item.ToString();
});
Console.WriteLine(string.Format("Parallel.ForEach: t{0} in t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
}
static void TestParallelFor(List<int> testData)
{
DateTime time1 = DateTime.Now;
Parallel.For(0, testData.Count, (i, loopState) => {
testData[i].ToString();
});
Console.WriteLine(string.Format("Parallel.For: t{0} in t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
}
static void TestFor(List<int> testData)
{
DateTime time1 = DateTime.Now;
foreach (var item in testData)
{
item.ToString();
}
Console.WriteLine(string.Format("ForEach: t{0} in t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
}
}
}
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace 24
{
class Program
{
static void Main(string[] args)
{
//Action[] actions = new Action[] {
//new Action(()=>{
// Console.WriteLine("hello");
//}),
//new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//}),
// new Action(()=>{
// Console.WriteLine("hello");
//})
//};
//for (int i = 0; i < actions.Length; i++)
//{
// actions[i]();
//}
并行循环
//Parallel.Invoke(actions);
//List<int> list = new List<int>{ 1, 2, 3, 6, 8, 56, 78, 95, 23, 45, 65, 21, 34, 67, 87 };
//Stopwatch watch = new Stopwatch();
//watch.Start();
Parallel.For(1, 11, i => {
Console.WriteLine(i + ",线程ID:" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
});
//Parallel.ForEach<int>(list, n => {
// Console.WriteLine(n);
// Thread.Sleep(1000);
//});
//watch.Stop();
//Console.WriteLine("OK");
//Console.WriteLine(watch.Elapsed);
//Console.ReadKey();
// 产生测试资料
List<int> testData = new List<int>();
Random rand = new Random();
// 产生乱数列表
for (int i = 0; i < 1000000; i++)
{
testData.Add(rand.Next(1000));
//Console.WriteLine(rand.Next(10));
}
// 打印正确结果
Console.WriteLine(testData.Sum());
for (int i = 0; i < 5; i++)
{
Console.WriteLine();
TestFor(testData);
TestParallelFor(testData);
TestParallelForEach(testData);
}
Console.ReadKey();
}
static void TestParallelForEach(List<int> testData)
{
DateTime time1 = DateTime.Now;
ConcurrentStack<int> resultData = new ConcurrentStack<int>();
Parallel.ForEach(testData, (item, loppState) =>
{
item.ToString();
});
Console.WriteLine(string.Format("Parallel.ForEach: t{0} in t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
}
static void TestParallelFor(List<int> testData)
{
DateTime time1 = DateTime.Now;
Parallel.For(0, testData.Count, (i, loopState) => {
testData[i].ToString();
});
Console.WriteLine(string.Format("Parallel.For: t{0} in t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
}
static void TestFor(List<int> testData)
{
DateTime time1 = DateTime.Now;
foreach (var item in testData)
{
item.ToString();
}
Console.WriteLine(string.Format("ForEach: t{0} in t{1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
}
}
}