Thread

T

Tony Johansson

Hello!

Here I have an example of a program that is using an additional worker
thread.
There is one thing here that is strange and that is how is it possible that
the worker thread
will finish it's work before the main thread if I enter 100 for the
interval.
As far as I know it's a heavy process to start a new Thread but here it
doesn't seem to take no time becuse the worker thread will fisish it's jobb
before the main thread.?

public class Test
{
static int interval;

static void DisplayNumbers()
{
Thread thisThread = Thread.CurrentThread;
string name = thisThread.Name;
Console.WriteLine("Starting thread: " + name);
Console.WriteLine(name + ": Current Culture = " +
thisThread.CurrentCulture);

for (int i = 1; i <= 8*interval; i++)
if (i % interval == 0) Console.WriteLine(name + ": count has
reached " + i);
}

static void StartMethod()
{
DisplayNumbers();
Console.WriteLine("Worker Thread Finished");
}

public static void Main()
{
Console.Write("interval to display results at?> ");
interval = int.Parse(Console.ReadLine());

Thread thisThread = Thread.CurrentThread;
thisThread.Name = "Main Thread";

ThreadStart workerStart = new ThreadStart(StartMethod);
Thread workerThread = new Thread(workerStart);
workerThread.Name = "Worker";
workerThread.Start();

DisplayNumbers();
Console.WriteLine("main Thread Finished");
Console.ReadLine();
}
}
 
T

Tony Johansson

Peter Duniho said:
Since both threads are using the same interval, it's always possible for
the new thread to finish before the original.

Creating a new thread is "heavy", to use a vague way of talking about it.
But that cost is borne on the original thread and is completed by the time
the Thread.Start() method returns. Then it's just a matter of when each
thread gets pre-empted.

For example, if the original thread is pre-empted just after creating the
new thread, but before it gets to do any work at all, then the new thread
gets a head start. Barring any other unusual interference with the
threads, at that point it's guaranteed to finish first.

In fact, I would say that the new thread on average is going to finish
first about half the time, because it always gets to start working on the
DisplayNumbers() method immediately, with its entire quantum left, while
the original thread is at best going to make a small amount of progress in
DisplayNumbers() the first time in, before it gets pre-empted to allow the
new thread to run.

In fact, with an interval of only 100, that's only 8000 iterations of the
loop, which is practically nothing on modern PC hardware. It's entirely
possible the new thread can finish the entire loop during its first
quantum, so unless the new thread can both create and start the new
thread, _and_ still finish its own 8000 iterations before the new thread
gets to run (a highly unlikely scenario), then the worker thread wins in
that scenario as well.

Pete

I mean it would be a heavy process to create a new thread with making
arrangement for the thread.
For example allocating various resources for the thread and performing
various security checks. This must take some time
so for me it would be much more sensible if the main thread would finish
before the worker thread.

There is a class Threadpool that is used among other things just becuse of
the heavy process to create thread from the Thread class.

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top