473,887 Members | 2,253 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 1547
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
1028
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 text is stored.
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 Me!lstProblemType.Selected(varItem) = True Then strDescr = strDescr & "," &
1
1475
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 should be displayed. I would like to know how to fetch the particular record as the service...
0
1980
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 "DefaultPageSettings.PaperSource.SourceName" is read only. How do I set it to the Multi-purpose Tray?
2
5327
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 following values with NO effect. web.config
3
2049
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
6586
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 print the names of all functions therein, in the following format:
4
2974
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 program will be used to input the data and the output will appear on the remote PC. I need help in...
2
1918
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 java.awt.*;
1
1576
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 bring another 3, and the process go one. Its something like - 1001 ...
1
10877
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10434
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7988
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4633
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
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.