Using Threads SWE 344 Internet Protocols & Client Server Programming.

8
Using Threads SWE 344 Internet Protocols & Client Server Programming

Transcript of Using Threads SWE 344 Internet Protocols & Client Server Programming.

Page 1: Using Threads SWE 344 Internet Protocols & Client Server Programming.

Using Threads

SWE 344Internet Protocols & Client Server

Programming

Page 2: 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.

Page 3: Using Threads SWE 344 Internet Protocols & Client Server Programming.

3

The Thread ClassThe 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() { . . }

Page 4: Using Threads SWE 344 Internet Protocols & Client Server Programming.

4

Thread Class Methods

Method Description

Abort() Terminates the threadEquals() 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

Page 5: Using Threads SWE 344 Internet Protocols & Client Server Programming.

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();

Page 6: Using Threads SWE 344 Internet Protocols & Client Server Programming.

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); }

} }

Page 7: Using Threads SWE 344 Internet Protocols & Client Server Programming.

7

Discuss:

-program that displays thread information for all the threads running on the system.

Threading in – TCP Server programUDP Server program

Page 8: Using Threads SWE 344 Internet Protocols & Client Server Programming.

8

END