Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Threads SWE 344 Internet Protocols & Client Server Programming.

Similar presentations


Presentation on theme: "Using Threads SWE 344 Internet Protocols & Client Server Programming."— Presentation transcript:

1 Using Threads SWE 344 Internet Protocols & Client Server Programming

2 What is a thread? A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations then it is often helpful to set different execution paths or threads, with each thread performing a particular job. Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application. To make a program execute more than one task at a time, it could be divided into smaller threads. The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution. 2

3 The Thread Class The C# language provides the System.Threading namespace, which includes classes for creating and controlling threads within a program. Use the Thread class to create a new Thread object, which produces a new thread within the current process. The format of the Thread constructor is as follows, where start is a ThreadStart delegate: Thread(ThreadStart start) The ThreadStart delegate points to the method that will be performed within the new thread. Here's an abbreviated example of creating a new thread:. Thread newThread = new Thread(new ThreadStart(newMethod));.. } void newMethod() {.. } 3

4 Thread Class Methods MethodDescription Abort()Terminates the thread Equals()Determines whether two Thread objects are the same GetHashCode()Gets a unique representation for the thread GetType()Gets the type of the current thread Interrupt()Interrupts a thread that is in the Wait thread state Join()Blocks the calling thread until the thread terminates Resume()Resumes a thread that has been suspended Start()Causes the operating system to change the thread state to Running Suspend()Suspends the execution of the thread ToString()Gets a string representation of the Thread object 4

5 The given example program creates two separate threads from the main program thread, using the Thread constructor (along with the ThreadStart constructor). The main program performs a for loop, displaying a counter value every second and using the Sleep() static method of the Thread class. The second thread does the same thing but uses a two-second delay; and the third thread uses a three-second delay. Again, remember that the threads start running only when the Start() method is used, not when the constructor finishes. using System; using System.Threading; class ThreadSample { public static void Main() { ThreadSample ts = new ThreadSample(); } public ThreadSample() { int i; Thread newCounter = new Thread(new ThreadStart(Counter)); Thread newCounter2 = new Thread(newThreadStart(Counter2)); newCounter.Start(); newCounter2.Start(); 5

6 for(i = 0; i < 10; i++) { Console.WriteLine("main: {0}", i); Thread.Sleep(1000); } void Counter() { int i; for (i = 0; i < 10; i++) { Console.WriteLine(" thread: {0}", i); Thread.Sleep(2000); } void Counter2() { int i; for (i = 0; i < 10; i++) { Console.WriteLine(" thread2: {0}", i); Thread.Sleep(3000); } 6

7 Discuss: -program that displays thread information for all the threads running on the system. Threading in – TCP Server program UDP Server program 7

8 END 8


Download ppt "Using Threads SWE 344 Internet Protocols & Client Server Programming."

Similar presentations


Ads by Google