o eşzamanlı orada parçacığı sayısını sayarak bir doğru yöntem olup olmadığını ben şüphe hala duyuyorum Parallel.ForEach keeps spawning new threads okumak zorunda? Gördüğüm ne.NET uygulamasında eşzamanlı iş parçacığı miktarı nasıl sayılır?
yöntem eş zamanlı olarak girilen ancak tamamlanmış yineleme sayısını (döngüler) Parallel.ForEach
sayar olmasıdır.
aynı anda çalıştırmak konuları doğru sayıda taşıma eşzamanlı iplik sayısının eşanlamlı mı?
Ben bir uzman değilim ama tahmin edebilirsiniz: aktivitesi daha sonra devam etmesi için bir yere takas ederken
- ipler yeniden kullanılabilir.
- teorik bir döngü aktivitesi için yapan iplik bir döngü tamamlanmasından sonra iplik havuzunda tutulur ama başka
- için yeniden kullanılabilir değildir ya da (sayım parçacığı), deney saflığını bozan olanakları ne olduğu?
Her neyse, .NET işleminin çalışan iş parçacıklarının miktarını, tercihen (C#) kodunda nasıl sayılır?
Güncelleme:
Yani, Jeppe Stig Nielsen's answer izleyin ve sonra sayımı
directThreadsCount = Process.GetCurrentProcess().Threads.Count;
çıkışı, hem sürümde (threadsCount == 7) ve hata ayıklama (threadsCount == 15 için kullanmak eğer
olduğunu[Job 0 complete. 2 threads remaining but directThreadsCount == 7
[Job 1 complete. 1 threads remaining but directThreadsCount == 7
[Job 2 complete. 2 threads remaining but directThreadsCount == 7
[Job 4 complete. 2 threads remaining but directThreadsCount == 7
[Job 5 complete. 2 threads remaining but directThreadsCount == 7
[Job 3 complete. 2 threads remaining but directThreadsCount == 7
[Job 6 complete. 2 threads remaining but directThreadsCount == 7
[Job 9 complete. 2 threads remaining but directThreadsCount == 7
[Job 7 complete. 1 threads remaining but directThreadsCount == 7
[Job 8 complete. 0 threads remaining but directThreadsCount == 7
FINISHED
, iplik sayısı hiç oanlatan azalan değildir:) modunda çok benzer"Class name is not valid at this point"
benim çıkarımlar doğru mudur verir System.Diagnostics.ProcessThread
ise yanlıştır ve neden kullanılamaz?ProcessThread
C# konsol uygulaması kullanılan kod:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Edit4Posting
{
public class Node
{
public Node Previous { get; private set; }
public Node(Node previous)
{
Previous = previous;
}
}
public class Edit4Posting
{
public static void Main(string[] args)
{
int concurrentThreads = 0;
int directThreadsCount = 0;
int diagThreadCount = 0;
var jobs = Enumerable.Range(0, 10);
Parallel.ForEach(jobs, delegate(int jobNr)
{
int threadsRemaining = Interlocked.Increment(ref concurrentThreads);
int heavyness = jobNr % 9;
//Give the processor and the garbage collector something to do...
List<Node> nodes = new List<Node>();
Node current = null;
//for (int y = 0; y < 1024 * 1024 * heavyness; y++)
for (int y = 0; y < 1024 * 24 * heavyness; y++)
{
current = new Node(current);
nodes.Add(current);
}
//*******************************
//uncommenting next line gives: "Class name is not valid at this point"
//diagThreadCount=System.Diagnostics.ProcessThread
directThreadsCount = Process.GetCurrentProcess().Threads.Count;
//*******************************
threadsRemaining = Interlocked.Decrement(ref concurrentThreads);
Console.WriteLine(
"[Job {0} complete. {1} threads remaining but directThreadsCount == {2}",
jobNr, threadsRemaining, directThreadsCount);
});
Console.WriteLine("FINISHED");
Console.ReadLine();
}
}
}
Teoride,' Thread's ve 'ProcessThread's farklı olabilir. Pratikte, neredeyse her zaman aynıdır. – svick
@svick Yukarıdaki özelliği yazan çok basit bir konsol uygulaması, '4', bazen' 3' verir. Tek iş parçacıklı bir uygulama olduğunu düşünürdüm. Aynı uygulama hata ayıklandığında (hata ayıklayıcı eklenmiş), sayı, 12 veya 13 gibi çok daha yüksektir.Başka bir uygulamada, 'System.Threading.Thread' sınıfı ile çok sayıda yeni iş parçacığı başlatırsam, System.Diagnostics.ProcessThread'in sayısı yaklaşık doğru sayıda artar. (Windows 7 64-bit çalıştı.) –