Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6 Threads Erick Pranata

Similar presentations


Presentation on theme: "Lecture 6 Threads Erick Pranata"— Presentation transcript:

1 Lecture 6 Threads Erick Pranata
Network Programming Lecture 6 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya

2 Drawbacks of Nonblocking I/O
Polling is inefficient Poll sooner, block happens Poll later, time is lost Cannot handle clients concurrently Handle one client at a time Known as iterative server Work best for clients that require small or bounded server connection time © Sekolah Tinggi Teknik Surabaya

3 Requirements Each connection needs to be processed independently
Threads: portions of code that can execute concurrently © Sekolah Tinggi Teknik Surabaya

4 Concurrent Server Thread-per-client
A new thread is spawn to handle new connection Or use thread pool, prespawned set of threads © Sekolah Tinggi Teknik Surabaya

5 Thread Example using System.Threading; // Create a ThreadStart instance // using your method as a delegate: ThreadStart methodDelegate = new ThreadStart(runMyThread); // Create a Thread instance using // your delegate method: Thread t = new Thread(methodDelegate); // Start the thread t.Start(); The new thread does not begin execution until its Start() method is invoked. When the Start() method of an instance of Thread is invoked, the CLR causes the specified method to be executed in a new thread, concurrently with all others. Meanwhile, the original thread returns from its call to Start() and continues its execution independently. (Note that directly calling the method without passing it to a Thread via a delegate has the normal procedure-call semantics: the method is executed in the caller’s thread.) The exact interleaving of thread execution is determined by several factors, including the implementation of the CLR, the load, the underlying OS, and the host configuration. For example, on a uniprocessor system, threads share the processor sequentially; on a multiprocessor system, multiple threads from the same application can run simultaneously on different processors. © Sekolah Tinggi Teknik Surabaya

6 Thread Example class MyThreadClass { private const int RANDOM_SLEEP_MAX = 500; LOOP_COUNT = 10; private String greeting; public MyThreadClass(String greeting) { this.greeting = greeting; } // Class that takes a String greeting as input, then outputs that // greeting to the console 10 times in its own thread with a random // interval between each greeting. Note that the method delegate cannot take any arguments or return a value. Luckily, there are mechanisms to circumvent both of these limitations. To pass arguments into a Thread instance while maintaining data encapsulation, you could break your separate thread code into its own class. For example, suppose you want to pass an instance of TcpClient into your runMyThread() method. You could create a new class (e.g., MyThread-Class) that contained the runMyThread() method, and pass the TcpClient instance into the class constructor. Then when you use a Thread to start the runMyThread() method, it can access the TcpClient instance via a local variable. © Sekolah Tinggi Teknik Surabaya

7 Thread Example public void runMyThread() { Random rand = new Random(); for (int x=0; x < LOOP_COUNT; x++) { Console.WriteLine( "Thread-" + Thread.CurrentThread.GetHashCode() + ": " + greeting ); try { Thread.Sleep( rand.Next(RANDOM_SLEEP_MAX) } catch (ThreadInterruptedException) {} } © Sekolah Tinggi Teknik Surabaya

8 Thread Example class ThreadExample { static void Main(string[] args) { MyThreadClass mtc1 = new MyThreadClass("Hello"); new Thread( new ThreadStart(mtc1.runMyThread) ).Start(); MyThreadClass mtc2 = new MyThreadClass("Aloha"); new ThreadStart(mtc2.runMyThread) MyThreadClass mtc3 = new MyThreadClass("Ciao"); new ThreadStart(mtc3.runMyThread) } © Sekolah Tinggi Teknik Surabaya

9 Concurrent Server Server Protocol Thread-per-client Thread Pool
Refer to handout © Sekolah Tinggi Teknik Surabaya

10 Referensi David Makofske, Michael J. Donahoo, Kenneth L. Calvert, TCP/IP Sockets in C#: Practical Guide for Programmers, Morgan Kaufmann, 2004 © Sekolah Tinggi Teknik Surabaya


Download ppt "Lecture 6 Threads Erick Pranata"

Similar presentations


Ads by Google