| I l @ ve RuBoard |
An Overview of Basic Threading Concepts
Visual Basic .NET and the .NET Framework provide a full set of threading capabilities that make creating and running simple threads very easy to do. But before we get into doing just that, let's take a step back and review how processes and threads are
Processes and Threads
Applications run in separate processes on a computer. For each new application launched, a new process is created. A process by definition contains at least one execution thread and has its own address space. This serves to isolate applications from each other because processes cannot share memory directly. Isolation
More
For ways to share resources across applications, see the discussion of remoting in Chapter 5.
Threads, on the other hand, are the basic unit to which an operating system allocates processor time. The operating system
The .NET Framework takes this model a step further by introducing the concept of application domains . An application domain is a lightweight, managed subprocess that can contain one or more threads. Threads within application domains can create additional application domains. Execution TimeThreads are granted execution time by the operating system in what are known as time slices . The duration of a time slice is system-dependent and varies between operating systems and processors. In the vast majority of environments, only one thread can be given execution control at a time. The exception is when threads are running on a multiprocessor machine. Limitations of Threads
Any given system will have an upper limit on the number of threads per process. On systems running Microsoft Windows XP, that limit is approximately 2000 threads per process. As a practical matter, the limit is actually much lower. Each thread
The memory commitment aside, tracking larger
The bottom line here is that you should create only as many threads as you need and no more. Due to the burden that additional threads impose on the system, you should make careful choices about what
Creating Threads
The
There are three important steps to creating any managed thread in Visual Basic .NET:
After following these steps, you'll have a thread that can be started and run on its own. The following example
ImportsSystem.Threading
ModuleBasicThreading
PublicConstMAX_COUNTAsInteger=200
SubMain()
'Createthethreadobjects
DimThread1AsNewThread(NewThreadStart(AddressOfThreadMethod1))
'WecanomittheNewThreadStartandjustusetheAddressOfoperator.
DimThread2AsNewThread(AddressOfThreadMethod2)
'Startthethreads
Thread1.Start()
Thread2.Start()
'Givetheuserachancetoreadtheinput
Console.ReadLine()
EndSub
'Therespectivethreadmethods
PublicSubThreadMethod1()
DimiAsInteger
Fori=0ToMAX_COUNT
Console.WriteLine("ThreadMethod1{0}",i)
Next
EndSub
PublicSubThreadMethod2()
DimiAsInteger
Fori=0ToMAX_COUNT
Console.WriteLine("ThreadMethod2{0}",i)
Next
EndSub
EndModule
When we run this application, the output looks like that shown in Figure 3-1. Both threads print out a repeated set of lines to the console. Note that your results (which thread will actually run first and which will finish first) might vary from run to run and will definitely vary from machine to machine. (Remember the three threading facts mentioned earlier.) Figure 3-1. Sample output of the Basic Threading sample.
The Basic Threading sample demonstrates the simplest form of threading: the thread class is given the address of a method that it will execute. The method itself has no notion of threads, nor can we pass any parameters to the thread method. One solution to this, given the simplest case, is to declare global variables that are used by your thread methods. This is not very elegant and can be very messy. This does not
The sample uses a global constant, MAX_COUNT , to pass information to the threads. The problem with this is that the mechanism is imprecise ”both of the thread methods use the same constant. But what if we want the threads to execute differently? What if we need to have each thread execute with different initial conditions and we want to create an arbitrary number of threads? Obviously, it gets much trickier using dedicated global variables ”we have to create specific variables for each thread, which can lead to a maintenance nightmare. What we need is a way to pass information to the thread execution methods without creating specific global variables for each thread. What we need is a better way to manage thread creation and execution: encapsulation . |
| I l @ ve RuBoard |