我正在运行此代码:
Parallel.For(0, 8, new ParallelOptions { MaxDegreeOfParallelism = 8 },
i => {
Console.WriteLine(i +
" : Thread ManagedThreadId: " + Thread.CurrentThread.ManagedThreadId +
" ID: " + AppDomain.GetCurrentThreadId().ToString());
}
)
输出:
1 : Thread ManagedThreadId: 10 ID: 9004
2 : Thread ManagedThreadId:10 ID: 9004
0 : Thread ManagedThreadId: 7 ID: 12884
4 : ThreadManagedThreadId: 7 ID: 12884
5 : Thread ManagedThreadId: 7 ID: 12884
6 : Thread ManagedThreadId: 7 ID: 12884
7 : Thread ManagedThreadId: 7 ID: 12884
3 : Thread ManagedThreadId: 10 ID: 9004
两个ID之间有什么区别?
答案 0 :(得分:1)
AppDomain.GetCurrentThreadId()
已过时:
'AppDomain.GetCurrentThreadId
已被弃用,因为当托管线程在光纤(又称轻量级线程)上运行时,它不提供稳定的ID。要获取托管线程的稳定标识符,请使用ManagedThreadId
上的Thread
属性。