473,387 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help needed with simple multi-threading example - reply to this message

My reply e-mail address was wrong in the prior message. Sorry about that.

The following sample code for the lock statement is on page 112 of the
O'Reilly book "C# Essentials". Can somebody explain to me why this
recursive class definition of LockTest does not cause an infinite number of
LockTest objects to be created (i.e., until it consumes all available
memory)? I don't understand why only two threads are created.
using System;
using System.Threading;

class LockTest
{
static void Main()
{
LockTest lt = new LockTest();
Thread t = new Thread(new ThreadStart(lt.Go));
t.Start();
lt.Go();
}
void Go()
{
lock(this)
for (char c='a'; c<='z'; c++)
Console.Write(c);
}
}
Nov 16 '05 #1
4 1513
Mark, all the 'Main' method does is run the instance method 'Go' on an
instance of the LockTest class twice, once through a the thread and once but
simply invoking the method. Go never calls itself it just locks using the
LockTest instance ('this') as the synchronication object and writes the
characters from a-z.

In order the app does this:
Create a new LockTest instance
Create a new threadstart delegate, passing the Go method as the target
Create a new thread
Start the Thread (thus invoking the Go method)
Invoke the Go method normally

Once both Go runs are finished, the program will exit.
HTH,
Richard

"Mark Huebner" <ne***@IntelligentSoftwareSystems.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
My reply e-mail address was wrong in the prior message. Sorry about that.

The following sample code for the lock statement is on page 112 of the
O'Reilly book "C# Essentials". Can somebody explain to me why this
recursive class definition of LockTest does not cause an infinite number of LockTest objects to be created (i.e., until it consumes all available
memory)? I don't understand why only two threads are created.
using System;
using System.Threading;

class LockTest
{
static void Main()
{
LockTest lt = new LockTest();
Thread t = new Thread(new ThreadStart(lt.Go));
t.Start();
lt.Go();
}
void Go()
{
lock(this)
for (char c='a'; c<='z'; c++)
Console.Write(c);
}
}

Nov 16 '05 #2
But why doesn't the new LockTest instance create a new LockTest instance
which creates a new LockTest instance ... etc.? Why doesn't the compiler
consider this true recursion? I'm still not getting it.

"Richard A. Lowe" <ch*****@YUMSPAMYUMYahoo.com> wrote in message
news:OC**************@TK2MSFTNGP10.phx.gbl...
Mark, all the 'Main' method does is run the instance method 'Go' on an
instance of the LockTest class twice, once through a the thread and once but simply invoking the method. Go never calls itself it just locks using the
LockTest instance ('this') as the synchronication object and writes the
characters from a-z.

In order the app does this:
Create a new LockTest instance
Create a new threadstart delegate, passing the Go method as the target
Create a new thread
Start the Thread (thus invoking the Go method)
Invoke the Go method normally

Once both Go runs are finished, the program will exit.
HTH,
Richard

"Mark Huebner" <ne***@IntelligentSoftwareSystems.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
My reply e-mail address was wrong in the prior message. Sorry about that.
The following sample code for the lock statement is on page 112 of the
O'Reilly book "C# Essentials". Can somebody explain to me why this
recursive class definition of LockTest does not cause an infinite number

of
LockTest objects to be created (i.e., until it consumes all available
memory)? I don't understand why only two threads are created.
using System;
using System.Threading;

class LockTest
{
static void Main()
{
LockTest lt = new LockTest();
Thread t = new Thread(new ThreadStart(lt.Go));
t.Start();
lt.Go();
}
void Go()
{
lock(this)
for (char c='a'; c<='z'; c++)
Console.Write(c);
}
}


Nov 16 '05 #3
Main is a static method. Nothing in your code calls Main. The Runtime
calls Main when the exe is started. That's your first thread.
In Main, another LockTest is created. All that runs automatically is the
constructor. You then create a new thread and call Go on the new thread.
That's your second thread and that's it.
There is nothing special about the Main mathod. You can create a Main
method on as many classes as you want and it will never be called unless
you specifically call it (especially as it's static). The one thing different
about it is that it is the default entry point for when the exe is started,
that's all.
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook

On 04 Jul 2004 07:30, "Mark Huebner" wrote:
But why doesn't the new LockTest instance create a new LockTest instance
which creates a new LockTest instance ... etc.? Why doesn't the compiler
consider this true recursion? I'm still not getting it.

"Richard A. Lowe" <ch*****@YUMSPAMYUMYahoo.com> wrote in message
news:OC**************@TK2MSFTNGP10.phx.gbl...
Mark, all the 'Main' method does is run the instance method 'Go' on an
instance of the LockTest class twice, once through a the thread and once

but
simply invoking the method. Go never calls itself it just locks using the
LockTest instance ('this') as the synchronication object and writes the
characters from a-z.

In order the app does this:
Create a new LockTest instance
Create a new threadstart delegate, passing the Go method as the target
Create a new thread
Start the Thread (thus invoking the Go method)
Invoke the Go method normally

Once both Go runs are finished, the program will exit.
HTH,
Richard

"Mark Huebner" <ne***@IntelligentSoftwareSystems.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
> My reply e-mail address was wrong in the prior message. Sorry aboutthat. >
> The following sample code for the lock statement is on page 112 of the
> O'Reilly book "C# Essentials". Can somebody explain to me why this
> recursive class definition of LockTest does not cause an infinite number

of
> LockTest objects to be created (i.e., until it consumes all available
> memory)? I don't understand why only two threads are created.
>
>
> using System;
> using System.Threading;
>
> class LockTest
> {
> static void Main()
> {
> LockTest lt = new LockTest();
> Thread t = new Thread(new ThreadStart(lt.Go));
> t.Start();
> lt.Go();
> }
> void Go()
> {
> lock(this)
> for (char c='a'; c<='z'; c++)
> Console.Write(c);
> }
> }
>


Nov 16 '05 #4
Because the Main method is the only method where a LockTest instance is
created, and this method is only invoked once, by the runtime, as the entry
point to your application. Main isn't called every time a LockTest instance
is created.

Richard

"Mark Huebner" <ne***@IntelligentSoftwareSystems.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
But why doesn't the new LockTest instance create a new LockTest instance
which creates a new LockTest instance ... etc.? Why doesn't the compiler
consider this true recursion? I'm still not getting it.

"Richard A. Lowe" <ch*****@YUMSPAMYUMYahoo.com> wrote in message
news:OC**************@TK2MSFTNGP10.phx.gbl...
Mark, all the 'Main' method does is run the instance method 'Go' on an
instance of the LockTest class twice, once through a the thread and once

but
simply invoking the method. Go never calls itself it just locks using the
LockTest instance ('this') as the synchronication object and writes the
characters from a-z.

In order the app does this:
Create a new LockTest instance
Create a new threadstart delegate, passing the Go method as the target
Create a new thread
Start the Thread (thus invoking the Go method)
Invoke the Go method normally

Once both Go runs are finished, the program will exit.
HTH,
Richard

"Mark Huebner" <ne***@IntelligentSoftwareSystems.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
My reply e-mail address was wrong in the prior message. Sorry about that.
The following sample code for the lock statement is on page 112 of the
O'Reilly book "C# Essentials". Can somebody explain to me why this
recursive class definition of LockTest does not cause an infinite

number of
LockTest objects to be created (i.e., until it consumes all available
memory)? I don't understand why only two threads are created.
using System;
using System.Threading;

class LockTest
{
static void Main()
{
LockTest lt = new LockTest();
Thread t = new Thread(new ThreadStart(lt.Go));
t.Start();
lt.Go();
}
void Go()
{
lock(this)
for (char c='a'; c<='z'; c++)
Console.Write(c);
}
}



Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Hal Robertson | last post by:
Hello, I would like to model and implement a simple application for keeping track of user survey data for our marketing department. The concept is very simple: a user of our website can opt-in...
1
by: NYprmr | last post by:
Hi All, I'm trying to get value from Listbox where Multi Select property is set to Simple, meaning multiple values can be selected. Doing regular loop like following, retuns NULL value for each...
1
by: VK | last post by:
Hello, I am currently doing a project on Access and am current stuck in the "search" process and hoping that you could help me out. I have got 3 tables designed. When the user clicks on search...
0
by: Paul Fredlein | last post by:
Hi, I writing a simple label printing programme just for my own use. It needs to print in "Landscape" with the paper source from the "Multi-purpose Tray" which I want to set programatically - I...
2
by: David M. Nolf | last post by:
I have a web service that has a method that takes a fair amount of time to complete. One of my clients is stating that they are getting a timeout back. I am having difficulties getting my...
3
by: Dara Durum | last post by:
Hi ! Py2.4, Win32. I need to optimize a program that have a speciality: hash (MD5/SHA1) the file contents (many files). Now I do this in a simple python program, because (what a pity) the...
121
by: jacob navia | last post by:
Hi guys I have written this small parser to print out the functions defined in a C file. This is an example of parsing in C, that I want to add to my tutorial. Comments (and bug reports) are...
4
by: perryschon | last post by:
Can someone please help me out with the Visual Basic source code needed that allows configuration and usage of comm ports in any PC? I am constructing an asynchronous serial communication RS-232...
2
by: deezle | last post by:
Hello, I am trying to get my calculator GUI to +,-,* and /. I have got all of them to work except my division. I was wondering if someone could helpme figure out the problem. Any input would help...
1
by: toskg | last post by:
I am seeking an Urgent Help from all my seniors and experts to complete my assignment. I am doing a Project for Multi-Level Marketing(MLM) Co. Website using ASP (DreamWeaver 8.0) and MS-Access as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.