我一直在网上搜索这个答案,但从那以后找不到真正有用的东西.
我有一个正在运行的程序,我想计算在给定时间我的方法中有多少个线程.
我的Main()函数中有代码:
Parallel.Invoke(MyMethod,MyMethod,MyMethod,MyMethod);
private static void MyMethod()
{
//how many threads are waiting here??? <--- this is what I am after
lock (myObj)
{
//one thread at a time please
}
}
Run Code Online (Sandbox Code Playgroud)
谁能在这里光明?
Jar*_*Par 12
无法直接查询给定函数中有多少个线程.唯一的方法是进行手动跟踪
private static int s_threadCount;
private static void MyMethod() {
Interlocked.Increment(ref s_threadCount);
try {
...
} finally {
Interlocked.Decrement(ref s_threadCount);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:如果可以递归地输入此方法,则无法准确计算线程数,而是计算线程数+递归进入函数的次数.
| 归档时间: |
|
| 查看次数: |
244 次 |
| 最近记录: |