473,549 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Threadin g;

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 1529
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***@Intellig entSoftwareSyst ems.com> wrote in message
news:u7******** ******@TK2MSFTN GP11.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.Threadin g;

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*****@YUMSPA MYUMYahoo.com> wrote in message
news:OC******** ******@TK2MSFTN GP10.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***@Intellig entSoftwareSyst ems.com> wrote in message
news:u7******** ******@TK2MSFTN GP11.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.Threadin g;

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*****@YUMSPA MYUMYahoo.com> wrote in message
news:OC******* *******@TK2MSFT NGP10.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***@Intellig entSoftwareSyst ems.com> wrote in message
news:u7******** ******@TK2MSFTN GP11.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.Threadin g;
>
> 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***@Intellig entSoftwareSyst ems.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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*****@YUMSPA MYUMYahoo.com> wrote in message
news:OC******** ******@TK2MSFTN GP10.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***@Intellig entSoftwareSyst ems.com> wrote in message
news:u7******** ******@TK2MSFTN GP11.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.Threadin g;

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
1014
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 to answer a short list of questions for data collection. The answers to all of the questions are presented in drop-down boxes, so no free form...
1
469
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 data item. ========================================== lCount = Me!lstProblemType.ListCount For varItem = 0 To lCount If...
1
1465
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 option based on the record # (not a combobox), services, location, date (all combo boxes) the particular record that is stored in the 'allinfo' table...
0
1972
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 don't want any page setup dialogs, I just want to click the "Print" button and it prints. I'm able to set Landscape but ...
2
5312
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 Windows XP workstation which is running the web service and a simple VB.NET test client to timeout on the method. I have looked and adjusted the...
3
2024
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 FSUM utility died in a file with unicode filename... (It is unknown error: I used alternate file name, but it not found).
121
6429
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 welcome. -------------------------------------------------------------cut here /* A simple scanner that will take a file of C source code and...
4
2964
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 circuit which will include serial cables (transmitter and receiver) connected from one pc to another pc with a 9-pin null modem in between. The VB...
2
1898
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 and I'll leave my code in here ========================================================= package chapfour; /** * * @author Deezle */ import...
1
1562
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 backend. In MLM business 1 referer introduce 3 associates under his downline and earn commission against that. Again that 3 associates eachone...
0
7462
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7492
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1957
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1069
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.