Willy,
The behavior seems to be related to how quickly the threads execute.
I modified your code, which uses a 2-second thread duration, to make 3
calls instead of two. All three were executed on a different thread.
However, I then changed it to a 0.2-second thread duration. This time
all 3 calls executed in sequence on the same thread.
And finally, I hunted for the sweet spot and found that with a 0.5
thread duration, the first and second calls executed on different
threads, but the 3rd executed on the same thread as the first.
Not sure why there is some delay in scheduling onto a new thread, but
it seems that the first request is executed immediately, the second is
executed after waiting a 2 or 3 tenths of a second, and the 3rd is
executed after waiting 5 tenthds of a second. Since all were
dispatched within microseconds of each other, I don't understand why
the delay in initiating their execution.
Perhaps this is a tunable policy of the thread pool manager???
--
Dave Hein
"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
[snip]
> Well, this code:
>
> using System;
> using System.Threading;
> namespace Willys
> {
> class Tester
> {
> static void Main()
> {
> Target t = new Target();
> t.SpawnEm();
> }
> }
> }
>
> public class Target {
> public delegate int LaunchProc(TimeSpan ts, out int v);
> public int TestMethod( TimeSpan callDuration, out int threadId )
> {
> threadId = AppDomain.GetCurrentThreadId();
> int myThreadId = threadId;
> int count = 10;
> TimeSpan subDuration = TimeSpan.FromTicks( callDuration.Ticks /count );
> Type t = this.GetType();
> for (int i = 0; i < count; ++i)
> {
> Thread.Sleep( subDuration );
> Console.WriteLine("[" + myThreadId.ToString() + "] "
> + t.FullName + ".TestMethod(): " + DateTime.Now.TimeOfDay + " "
> + i.ToString() );
> }
> return threadId;
> }
> public void SpawnEm() {
> int count = 2;
> IAsyncResult[] asr = new IAsyncResult[count];
> int threadId = 0;
> LaunchProc pfn = new LaunchProc(this.TestMethod);
> for (int t = 0; t< count; t++) {
> asr[t] = pfn.BeginInvoke(new TimeSpan(20000000),out threadId, null,
> null);
> }
> foreach(IAsyncResult ar in asr)
> {
> pfn.EndInvoke(out threadId, ar);
> Console.WriteLine("Thread [{0}] Done", threadId);
> }
> }
> }
>
> Gives this:
[snip]
> Thread [2856] Done
[snip]
> Thread [2976] Done
>
> So you see clearly that two threadpool threads are activated.
>
> Willy.
|