School of Information Technologies Multithreading NETS3303/3603 Week 3.

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Socket Programming.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
Concurrency…leading up to writing a web crawler. Web crawlers.
1 Multithreading. 2 Threads Program units that execute independently; multiple threads run “simultaneously” Virtual machine executes each thread for short.
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
System Programming Practical session 10 Java sockets.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Fundamentals of Python: From First Programs Through Data Structures
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Ryerson University CPS Distributing Computing with Java.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
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.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Java Server Programming Web Interface for Java Programs.
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.
Part 4: Network Applications Client-server interaction, example applications.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Java Packages Thread Example Java Socket Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Network Programming. These days almost all devices.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Lecture 21 Sockets 1 (Not in D&D) Date.
Networking with Java 2.
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Multithreading.
Multithreading.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Multiplexing/Demux.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

School of Information Technologies Multithreading NETS3303/3603 Week 3

School of Information Technologies 2 Lesson Outcome Process vs Thread Creating threads: –Thread Object –runnable interface Concurrent connection-oriented server Handling timeouts gracefully Locks for controlling access to shared resources

School of Information Technologies 3 Multithreading Many programs need to do more than one tasks concurrently –A gui program display animation in background while processing user foreground interactions –A web browser may download a graphics file while rendering the rest of the page Similarly, a client/server application may handle hundreds of clients simultaneously Previously, we designed iterative server, now need design concurrent

School of Information Technologies 4 Process Most OSes distinguish a program from a process A program is the code, a process is an instance of a program created when it is run Copy of a processes is possible => child process –In Unix, pid and ppid are used to relate parent and child processes –Both have same image #include int main (void) { pid_t pid; printf("Hello World\n”); pid = fork(); printf("Bye World\n"); return 0; } Hello World Bye World

School of Information Technologies 5 Process vs Thread New process require fresh memory block –Self-contained execution environment –Server’s resources will exhaust, if use multi-process approach! Use thread – a flow of control –Shares resources with main process (including memory and open files) –A lightweight process! In Java, a thread is associated with an instance of the class Thread

School of Information Technologies 6 Thread There are two ways to do this: –Provide a Runnable object –The Runnable interface defines a single method, run, meant to contain the code executed in the thread public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); }

School of Information Technologies 7 Thread II 2 nd approach: extend Thread The Thread class itself implements Runnable, though its run method does nothing –providing its own implementation of run public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); }

School of Information Technologies 8 Thread III Thread defines a number of methods useful for thread management Includes static methods, which provide info, or affect the status Certain methods are invoked from other threads involved in managing the thread and Thread object

School of Information Technologies 9 Pausing Execution with Sleep Thread.sleep causes the current thread to suspend execution for a specified period –making processor time available to the other threads – waiting for another thread –sleep period can be terminated by interrupts! –throws a checked exception (an InterruptedException ) join allows one thread to wait for the completion of another –If t is a Thread object whose thread is currently executing: t.join(); –causes the current thread to pause execution until t 's thread terminates

School of Information Technologies 10 Example: 2 threads Two threads sleep random times and display their names between sleeps public class ThreadShowName extends Thread { public static void main(String[] args) { ThreadShowName thread1, thread2; thread1 = new ThreadShowName(); thread2 = new ThreadShowName(); thread1.start(); thread2.start(); } public void run() { int pause; for (int i=0; i<10; i++) { try { System.out.println(getName() + " executed."); pause = (int)(Math.random() * 3000); sleep(pause); } catch (InterruptedException e) {} } } Thread-0 executed. Thread-1 executed. Thread-0 executed. Thread-1 executed. Thread-0 executed. Thread-1 executed. Thread-0 executed. Thread-1 executed. Thread-0 executed. Thread-1 executed. Thread-0 executed. …

School of Information Technologies 11 Same example with runnable public class RunnableHelloCount implements Runnable { private Thread thread1, thread2; public static void main(String[] args) { RunnableHelloCount threadDemo = new RunnableHelloCount(); } public RunnableHelloCount() { thread1 = new Thread(this); thread2 = new Thread(this); thread1.start(); thread2.start(); } public void run() { int pause; for (int i=0; i<10; i++) { try { System.out.println(Thread.currentThread().getName()+ " executed."); pause = (int)(Math.random()*3000); Thread.sleep(pause); } catch (InterruptedException e) {} }

School of Information Technologies 12 Multithreaded Server Most real-world applications need to handle many connections concurrently Involves a two-stage process: –Main threads allocates individual threads to incoming clients –These threads handle all subsequent interaction between that client and server Any problem with a client, does not affect others!

School of Information Technologies 13 Revisit TCPEchoServer Server echoes messages back to multiple clients Use a support class ClientHandler –Has a reference to the relevant client socket To changes to the TCPEchoClient Can control requests to backlog (request queue!) –ServerSocket(int port, int backlog)

School of Information Technologies 14 import java.io.*; import java.net.*; public class MultiEchoServer { private static ServerSocket servSocket; private static final int PORT = 1234; public static void main(String[] args) throws IOException { try { servSocket = new ServerSocket(PORT, 10); } catch (IOException e){ System.out.println("\nUnable to set up port!"); System.exit(1); } do { //Wait for client... Socket client = servSocket.accept(); System.out.println("\nNew client accepted.\n"); //Create a thread to handle communication with //this client and pass the constructor for this //thread a reference to the relevant socket... ClientHandler handler = new ClientHandler(client); handler.start();//As usual, this method calls run. } while (true); }

School of Information Technologies 15 class ClientHandler extends Thread { private Socket client; private BufferedReader in; private PrintWriter out; public ClientHandler(Socket socket) { //Set up reference to associated socket... client = socket; try { in = new BufferedReader(new InputStreamReader( client.getInputStream())); out = new PrintWriter( client.getOutputStream(),true); } catch(IOException e){} } public void run() { try { String received; do { received = in.readLine(); //Echo message back to client on //the socket's output stream... out.println("ECHO: " + received); } while (!received.equals("QUIT")); } catch(IOException e){ } finally {/* same as before */ } }

School of Information Technologies 16 public class MultiEchoClient { private static InetAddress host; private static final int PORT = 1234; private static Socket link; private static BufferedReader in; private static PrintWriter out; private static BufferedReader keyboard; public static void main(String[] args) throws IOException { try { host = InetAddress.getLocalHost(); link = new Socket(host, PORT); in = new BufferedReader(new InputStreamReader( link.getInputStream())); out = new PrintWriter(link.getOutputStream(),true); keyboard = new BufferedReader( new InputStreamReader(System.in)); String message, response; do { System.out.print("Enter message ('QUIT' to exit): "); message = keyboard.readLine(); //Send message to server on out.println(message); //read reply from input stream... response = in.readLine(); System.out.println(response); }while (!message.equals("QUIT")); } … }

School of Information Technologies 17 Sample interaction C:\java MultiEchoServer New client accepted. Closing down connection... C:\java MultiEchoClient Enter message ('QUIT' to exit): hi 1 ECHO: hi 1 Enter message ('QUIT' to exit): bye 1 ECHO: bye 1 Enter message ('QUIT' to exit): QUIT ECHO: QUIT Closing down connection... C:\java MultiEchoClient Enter message ('QUIT' to exit): hi 2 ECHO: hi 2 Enter message ('QUIT' to exit): bye 2 ECHO: bye 2 Enter message ('QUIT' to exit): QUIT ECHO: QUIT Closing down connection...

School of Information Technologies 18 Handling network timeouts Writing network apps in a controlled environment, timeout is not an issue But on Internet, slow connections, link failure, node failure, congestion may happen –client can freeze and server threads block indefinitely With I/O, there are two classifications of operations: –Blocking operations: Read or write stalls, operation waits until I/O device is ready –Nonblocking operations: Read or write attempt is made, operation aborts if I/O device is not ready Java networking is a form of blocking I/O

School of Information Technologies 19 Socket timeout option to rescue java.net allows programmers to specify socket options SO_TIMEOUT, is extremely useful, –to specify the amount of time that a read operation will block setSoTimeout ( int ) is available for: –java.net.Socket –java.net.DatagramSocket –java.net.ServerSocket –specify a maximum timeout length, in milliseconds ServerSocket.accept(), SocketInputStream.read(), DatagramSocket.receive() –Whenever one of these methods is called, the clock starts ticking –If the operation is not blocked, it will reset and only restart once one of these methods is called again –So can handle in both client and server

School of Information Technologies 20 Timeout for UDP prevents our network operations from blocking indefinitely What happens when a network operation times out? –throws a java.io.InterruptedIOException // Create a datagram socket on port // 2000 to listen for incoming UDP packets DatagramSocket dgramSocket = new DatagramSocket (2000); // Disable blocking I/O operations by specifying a five second timeout dgramSocket.setSoTimeout (5000);

School of Information Technologies 21 Timeout for TCP? // Set the socket timeout for ten seconds connection.setSoTimeout (10000); try { // Create a DataInputStream for reading from socket DataInputStream din = new DataInputStream (connection.getInputStream()); // Read data until end of data for (;;) { String line = din.readLine(); if (line != null) System.out.println (line); else break; } // Exception thrown when network timeout occurs catch (InterruptedIOException iioe) { System.err.println ( "Remote host timed out during read operation"); } // Exception thrown when general network I/O error occurs catch (IOException ioe){ }

School of Information Technologies 22 Example: Mulithreaded Echo Server with timeout It limits number of connections and rejects further requests When read does not succeed within time, it shuts down the individual thread To test echo server, use telnet command as client program to port 2000 of your local machine –Type something and see what happens after 30 sec of inactivity –Server disconnects telnet client!

School of Information Technologies 23 public class EchoServer extends Thread { private Socket m_connection; private static int number_of_connections = 0; private static final int max_connections = 2; private static final int timeout_length = 30000; public EchoServer (Socket connection) { m_connection = connection; // Set a timeout of 'timeout_length' milliseconds try { m_connection.setSoTimeout (timeout_length); } catch (SocketException se) { } number_of_connections++; } public void run() { try { // Get I/O streams InputStream in = m_connection.getInputStream(); OutputStream out= m_connection.getOutputStream(); try { for (;;) // Echo data straight back to client out.write(in.read()); } catch (InterruptedIOException iioe) { } } catch (IOException ioe) { } number_of_connections--; }

School of Information Technologies 24 /* continue from previous slide… */ public static void main(String args[]) throws Exception { // Bind to a local port ServerSocket server = new ServerSocket (service_port); for (;;) { // Accept the next connection Socket connection = server.accept(); // Check to see if maximum reached if (number_of_connections > max_connections-1) { // Kill the connection PrintStream pout = new PrintStream (connection.getOutputStream()); pout.println ("Too many users"); connection.close(); continue; } else { // Launch a new thread new EchoServer(connection).start(); }

School of Information Technologies 25 Sample Output C:\java EchoServer Timeout occurred - killing connection C:\telnet localhost C:\telnet localhost Connection to host lost. C:\telnet localhost 2000 Too many users Client 1 Client 2 Client 3

School of Information Technologies 26 Locks and Deadlocks: Briefly Multithreaded apps present awkward problems –Need to coordinate activities of threads –Shared data need to be managed –Each operation need to be atomic (if one operation can be split up into many simpler operations) –add requires read current value, create a copy, add then write back 23 thread1 add 5 thread2 add 19

School of Information Technologies 27 Locks and Deadlocks II Solution: thread should obtain a lock on the object Only then, can modify the object After write, release for others to use But without proper coordination, threads may go into deadlock! –Threads wait for events that will never occur. res1 res2 thread1thread2 has lock needs lockhas lock needs lock

School of Information Technologies 28 Synchronizing threads Locking achieved by placing keyword synchronized in front of the method definition or block of code –When this invoked, a thread locks out others Other thread must wait If a thread cannot proceed, it calls wait to release lock When a synchronized method is completed, call notify or notifyall to awake others wait, notify and notifyall is executed when a thread has lock on an object

School of Information Technologies 29 Lock in action class Resource { private int numResources; private final int MAX = 5; public Resource(int startLevel) { numResources = startLevel; } public int getLevel() { return numResources; } public synchronized int addOne() { try { while (numResources >= MAX) wait(); numResources++; //'Wake up' waiting a consumer... notify(); } catch (InterruptedException e) { } return numResources; } public synchronized int takeOne() { try { while (numResources == 0) wait(); numResources--; //'Wake up' any waiting producer... notify(); } catch (InterruptedException e) {} return numResources; }

School of Information Technologies 30 Summary Use threads to handle different clients to enable concurrent processing Timeouts are easy to handle for any sockets However, when client threads access shared resources, mutable operations need to be properly synchronized! Next: Network layer protocols

School of Information Technologies 31 References cles/Networking/timeouts/ cles/Networking/timeouts/ twork_timeouts/ twork_timeouts/