Lecture 6 Threads Erick Pranata

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Advertisements

Lecture 1 Overview of Socket Programming Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Lecture 2b Sockets Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Concurrency…leading up to writing a web crawler. Web crawlers.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Computer Science Lecture 2, page 1 CS677: Distributed OS Last Class: Introduction Distributed Systems – A collection of independent computers that appears.
TCP Socket Programming CPSC 441 Department of Computer Science University of Calgary.
OOP&M - theory lectures1 OOP&M – and there it was an eighth day “I f you know what I know, what am I doing here? ” - Aristoteles -
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 19 Microsoft’s Approach 1 –.NET Mobile Framework part 2 Rob.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Classes SWE 344 Internet Protocols & Client Server Programming.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
1 Web Based Programming Section 8 James King 12 August 2003.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Laboratory - 4.  Threading Concept  Threading in.NET  Multi-Threaded Socket  Example.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Sistem Operasi © Sekolah Tinggi Teknik Surabaya 1.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 18 Microsoft’s Approach 1 –.NET Mobile Framework part 2 Rob.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lecture 20 Threads Richard Gesick. Threads Makes use of multiple processors/cores if available Share memory within the program (as opposed to processes.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Classes - Intermediate
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
Threads in Java Two ways to start a thread
CS399 New Beginnings Jonathan Walpole.
CS1101: Programming Methodology Recitation 7 – Exceptions
Multithreaded Programming in Java
CSE 451: Operating Systems Winter 2006 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
More examples How many processes does this piece of code create?
Condition Variables and Producer/Consumer
CSE 451: Operating Systems Autumn 2003 Lecture 16 RPC
Threads and Concurrency in Java: Part 1
Condition Variables and Producer/Consumer
CSE 451: Operating Systems Winter 2007 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Multithreading.
CSE 451: Operating Systems Winter 2004 Module 19 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Spring 2012 Module 22 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Autumn 2009 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSCD 330 Network Programming
.Net Sockets.
Chapter 4 Threads, SMP, and Microkernels
Method of Classes Chapter 7, page 155 Lecture /4/6.
Threads and Multithreading
CSE 451: Operating Systems Autumn 2010 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Multiplexing/Demux.
CSE 451: Operating Systems Winter 2003 Lecture 16 RPC
CMSC 202 Exceptions.
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
CMSC 202 Threads.
CSE 451: Operating Systems Messaging and Remote Procedure Call (RPC)
CSG2H3 Object Oriented Programming
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2008
Presentation transcript:

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

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

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

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

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

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

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

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

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

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