NET服务端开发—多线程使用小结(多线程使用常识)
来源:互联网 发布:免费负载均衡软件 编辑:程序博客网 时间:2024/05/29 18:04
1.使用入口线程来处理超长时间调用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public
class
ProductApplicationService
{
public
void
SyncProducts()
{
Task.Factory.StartNew(()
=>
{
var
productColl =
DominModel.Products.GetActivateProducts();
if
(!productColl.Any())
return
;
DominModel.Products.WriteProudcts(productColl);
});
}
}
1.1异常处理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public
class
ProductApplicationService
{
public
void
SyncProducts()
{
try
{
Task.Factory.StartNew(() =>
{
var
productColl = DominModel.Products.GetActivateProducts();
if
(!productColl.Any())
return
;
DominModel.Products.WriteProudcts(productColl);
});
}
catch
(Exception exception)
{
//记录下来...
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public
class
ProductApplicationService
{
public
void
SyncProducts()
{
Task.Factory.StartNew(SyncPrdoctsTask);
}
private
static
void
SyncPrdoctsTask()
{
try
{
var
productColl =
DominModel.Products.GetActivateProducts();
if
(!productColl.Any())
return
;
DominModel.Products.WriteProudcts(productColl);
}
catch
(Exception exception)
{
//记录下来...
}
}
}
2.利用并行来提高多组数据的读取
1
2
3
4
5
6
7
8
9
10
public
static
ProductCollection GetProductByIds(List<
long
>
pIds)
{
var
result =
new
ProductCollection();
Parallel.ForEach(pIds, id =>
{
//并行方法
});
return
result;
}
2.1控制并行线程数:
1
2
3
4
5
6
7
8
9
10
11
public
static
ProductCollection GetProductByIds(List<
long
>
pIds)
{
var
result =
new
ProductCollection();
Parallel.ForEach(pIds,
new
ParallelOptions() {
MaxDegreeOfParallelism = 5
/*设置最大线程数*/
}, id =>
{
//并行方法
});
return
result;
}
2.2使用并行处理时数据的前后顺序是第一原则
3.手动开启一个线程来代替并行库启动的线程
1
2
3
4
public
async Task<
int
> OperationProduct(
long
ids)
{
return
await DominModel.Products.OperationProduct(ids);
}
1
2
3
4
5
6
7
8
9
10
public
static
async Task<
int
> OperationProduct(
long
ids)
{
return
await Task.Factory.StartNew<
int
>(() =>
{
System.Threading.Thread.Sleep(5000);
return
100;
//其实这里开启的线程是请求线程池中的请求处理线程,说白了这样并不会提高并发等于没用。
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public
async Task<
int
> OperationProduct(
long
id)
{
var
funResult =
new
AWaitTaskResultValues<
int
>();
return
await DominModel.Products.OperationProduct(id, funResult);
}
public
static
Task<
int
> OperationProduct(
long
id,
AWaitTaskResultValues<
int
> result)
{
var
taskMock =
new
Task<
int
>(() => {
return
0;
});
//只是一个await模拟对象,主要是让系统回收当前“请求处理线程”
var
thread =
new
Thread((threadIds)
=>
{
Thread.Sleep(7000);
result.ResultValue = 100;
taskMock.Start();
//由于没有任何的逻辑,所以处理会很快完成。
});
thread.Start();
return
taskMock;
}
0 0
- NET服务端开发—多线程使用小结(多线程使用常识)
- .NET应用架构设计—服务端多线程使用小结(多线程使用常识)
- 使用VB.NET开发多线程
- 使用VB.NET开发多线程
- 使用VB.NET开发多线程
- C#.net使用多线程
- ASP.NET使用多线程
- [vb.net]使用多线程
- C# WPF 多线程使用小结
- 【iOS开发-多线程】使用NSThread创建多线程
- iOS多线程开发——GCD的使用与多线程开发浅析(二)
- iOS多线程的开发——GCD的使用与多线程开发浅析(二)
- vb.net中使用多线程
- asp.net中使用多线程
- .net中多线程的使用
- 如何使用vb.net多线程
- 使用 .NET Compact Framework 2.0 开发多线程应用程序
- iOS开发—使用NSThread实现多线程
- #10 D. LCIS (dp+最长公共上升子序列)
- HOW TO TALK ABOUT VARIOUS PRACTIONERS学习笔记
- 使用DEAP基于K-means的情感计算
- hdu1015
- kill –HUP 1234 >killout.txt 2 >killerr.txt
- NET服务端开发—多线程使用小结(多线程使用常识)
- VMware添加新硬盘
- html5 meta(移动端)介绍及使用
- hzau 1207 Candies(线段树 区间更新 区间查询)
- codeforces Kidnapping
- 移动端开发遇到的坑
- 【java并发】基础(2)--线程中断和终止
- 剑指offer(C++)——重建二叉树
- bug小能手