3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelForEachDemo
{
public class Store
{
public int Id { get; set; }
public string Domain { get; set; }
public int SleepTime { get; set; }
}
public class Program
{
static void Main(string[] args)
{
var stores = new List<Store> {
new Store{Id=1,Domain="yahoo.com",SleepTime=6000},
new Store{Id=2,Domain="google.com",SleepTime=5000},
new Store{Id=3,Domain="baidu.com",SleepTime=4000},
new Store{Id=4,Domain="sina.com.cn",SleepTime=3000},
new Store{Id=5,Domain="163.com",SleepTime=2000},
new Store{Id=6,Domain="21cn.com",SleepTime=10000}
};
Console.ReadKey();
var ids = GetIds(stores);
Console.WriteLine("Id list:{0}", string.Join(",", ids));
Console.ReadKey();
}
private static CancellationTokenSource cts = new CancellationTokenSource();
static List<int> GetIds(List<Store> stores)
{
var ids = new List<int>();
var threadCounter = 0;
var token = cts.Token;
try
{
var pr = Parallel.ForEach(stores, new ParallelOptions { MaxDegreeOfParallelism = 10, CancellationToken = token }, store =>
{
threadCounter++;
Console.WriteLine("Current threads:{0},Current thread id:{1}", threadCounter, Thread.CurrentThread.ManagedThreadId);
//var rand = new Random();
//var sleep = rand.Next(100, 10000);
if (threadCounter > 3)
{
cts.Cancel(true);
}
Console.WriteLine("Store id:{0},sleep seconds:{1}", store.Id, store.SleepTime);
Thread.Sleep(store.SleepTime);
Console.WriteLine("Store id:{0} completed!!!", store.Id);
ids.Add(store.Id);
});
Console.WriteLine("ALL STORES ARE COMPLETED?{0}", pr.IsCompleted);
}
catch (OperationCanceledException ex)
{
Console.WriteLine("Task was canceled!!!{0}", ex.Message);
}
//Console.WriteLine("All stores are completed!!!");
return ids;
}
}
}
|
√
8年專業(yè)做網(wǎng)站經(jīng)驗
幫助您解決各種需求,并得到滿意方案
√
效果滿意后付款
預付30%定金,效果讓您滿意后,付全款
√
資深用戶體驗設計師
秉承"一切以用戶價值為依歸"理念,重視用戶體驗,專為企業(yè)需求進行定制
√
采用國際標準結構建站
DIV+CSS國際標準構建網(wǎng)頁,可有效提高網(wǎng)站訪問速度、提高網(wǎng)站頁面的可維護
√
實現(xiàn)網(wǎng)絡營銷[SEO]最大化
網(wǎng)站程序中融入SEO,有效降低企業(yè)成本