REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
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.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Multithreading. 2 Threads Program units that execute independently; multiple threads run “simultaneously” Virtual machine executes each thread for short.
Thread Examples. Runnable Interface Runnable defines only one abstract method; Public void run(); Thread also implements Runnable interface. Why does.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Cosc 4755 Phone programming: GUI Concepts & Threads.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
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.
Networking with Java. Basic Concepts A Network exists when two or more computers are connected such that they can communicate data back and forth. There.
Fundamentals of Python: From First Programs Through Data Structures
Multithreading.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
By: Joel Rodriguez.  International student from Mexico  Delicias, Chihuahua Mexico  Spanish  Sports and Music.
Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
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.
Sockets Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection, the client side and the server.
Threading Eriq Muhammad Adams J
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
Dynamic Architectures (Component Reconfiguration) with Fractal.
Concurrent Programming and Threads Threads Blocking a User Interface.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Field Trip #25 Creating a Client/Server By Keith Lynn.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threading and Concurrency COM379T John Murray –
Java Server Programming Web Interface for Java Programs.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
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.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
Threads CS 21b. Threads in Java zDefinition Õunit of program control that represents an execution sequence zJava supports multi-threaded execution * Programs.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
Threads in Java Two ways to start a thread
Lecture 21 Sockets 1 (Not in D&D) Date.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
CSCD 330 Network Programming
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Threads and Multithreading
Representation and Management of Data on the Internet
Threads.
Gentle Introduction to Threads
Multiplexing/Demux.
Lecture 19 Threads CSE /6/2019.
Presentation transcript:

REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait for connections using the accept( ) method. The accept method returns the actual Socket object used by the server to communicate with the client.

Review The Client side of your program needs only to open a Socket. It does this by creating a new Socket object and passing both an IP address ( for us we will use (the localhost)) as well as a port number (the same port number the server is bound to). Both the client and server communicate via their Sockets using a Printwriter (to send info) or a BufferedReader (to receive info).

Multitasking Doing more than one thing at a time…

Threads Until now, every program you have written has had a single “thread” of control. This simply means that if you could stop time at any particular instant your program would be in the process of executing one and only one instruction. Today, many programs have multiple threads of control at any given time. This makes them more efficient.

Threads Consider a program such as a word processor. While you print your large document you can continue to edit your text. Similarly, a web server should be able to handle many different requests from many different users at the same time. Many programs will use one thread for the GUI and another to carry out the user’s requests.

Threads in Java Our Client-Server model is often implemented using a “multithreaded” server. If we implement our Server using only one thread then what happens if multiple clients try to connect? Think about it – what happens?

Threaded Servers What happens is the first client will connect to the server and have its complete attention. The other clients will have to wait. If the first client has a very long request or many different requests then the others may wait a long time to get the server’s attention. Imagine if a web server worked like that….

Multithreading To get around this problem, programmers often code their servers so that they are multithreaded. Each new client gets its own thread to handle its requests. A web server for example can handle many different clients at one time rather than making all but one wait for service.

Threads Before we look at implementing a multithreaded server we will take a look at how to implement threads in general and try to see how they work…. There are two ways to make a class multithreaded. Which of the two ways we use depends upon the particular class we are considering.

Extending the Thread Class If the class we wish to multithread does not inherit from another class ( that is, if it does NOT extend a class) then we can simply have our new class extend the Java class Thread. We’ve seen this idea of extending a class when we looked at building GUIs and extended the JPanel class.

Extending the Thread class Below is a sample of what our class declaration will look like… public class SimpleThread extends Thread { private int name; public SimpleThread(int x) { name = x; } // constructor public int getname( ) { return ( name ); }

Extending Thread class Run method is called when Thread is started public void run() { for (int i = 1; i <= 10; i++) { System.out.println(“ Turn “ + i + " Thread number " + getName()); try { sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {// do nothing} } // end for loop System.out.println("DONE! Thread number " + getName()); } // end run method

Thread Example We can use the following Main method to test our simpleThread class. public static void main( String args[ ]) { SimpleThread th = null; for (int i = 0; i < 10; i++ ) { th = new SimpleThread( i ); th.start(); // calls the run( ) method of the thread } // This program creates 10 threads and starts em running

Thread Example Our program creates and starts 10 Threads (named 0-9). The threads then each begin executing when the main calls start( ) on them. Each thread is written to go through 10 turns where they print the turn # and their name. The threads “sleep” for a random period of time before they wake up and do the next turn.

Thread exmple So if we look at any single thread (say Thread 5) it simply should print Turn 1 Thread Number 5 Turn 2 Thread Number 5 … Turn 10 Thread Number 5 DONE! Thread Number 5 BUT all ten threads (0-9) are running at once! So our output should be mixed up between all threads. Let’s try it and see what we get…

Second Example – Simple Server Our second example will build a simple multithreaded server. We will use our High-Low guessing game program turn it into multithreaded server capable of playing the game with as many clients as we wish to start up.

Threaded Servers The basic idea of a threaded server is to have the server program listen at its port and when a connection comes in it should take the resulting Socket object and pass it along to the constructor of a new threaded object to actually play the game with the client. The server then returns to listen for further connections while the thread plays the game with the client/user.

Single Client Guessing Game Let’s take a look at the solution for the single client version we worked on last time… Ask about anything that doesn’t make sense or is not clear!!!

Threaded Guessing server We’ll use two classes. The first will bind to port 4321 and listen for connections. Once a client connects, this server will create a new thread and pass it the Socket object and will then go back to listening for more connections. The threaded class will receive a Socket and will actually carry out the game itself with the given client.

Implementing runnable As mentioned earlier, extending the Thread class only works if our class does not already extend some other class – this is because Java only allows a class to extend one class. To get around this stupid design choice, Java does allow us to “implement” an interface. You saw this when you played around with Graphics. This causes us a problem when we wish to make a class Threaded but it already extends something like say JPanel or something similar.

Runnable To Thread an already extended class simply have the class implement the runnable interface and add a run( ) method just as we saw in our earlier examples. We create an object of the class and then pass it to the constructor for Thread( ). The resulting Thread object can then be “started” via the.start( ) method.

Runnable Class Example class runexpl implements Runnable { // run() method is called when the thread runs public void run() { //do stuff here } } // below is how we’d use it runexpl runnable = new runexpl(); // Create the thread supplying the runnable object Thread mythread = new Thread(runnable); // Start the thread mythread.start();