Chapter 15 Multithreading, Networks, and Client/Server Programming

Slides:



Advertisements
Similar presentations
Threads Chapter 4 Threads are a subdivision of processes
Advertisements

1. XP 2 * The Web is a collection of files that reside on computers, called Web servers. * Web servers are connected to each other through the Internet.
1 Senn, Information Technology, 3 rd Edition © 2004 Pearson Prentice Hall James A. Senns Information Technology, 3 rd Edition Chapter 7 Enterprise Databases.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Process Description and Control
1 Concurrency: Deadlock and Starvation Chapter 6.
Zhongxing Telecom Pakistan (Pvt.) Ltd
1 Multithreaded Programming in Java. 2 Agenda Introduction Thread Applications Defining Threads Java Threads and States Examples.
1 Applets Programming Enabling Application Delivery Via the Web.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Processes and Operating Systems
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 1 Embedded Computing.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
18 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Introduction to Web Services.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Year 6 mental test 5 second questions
So far Binary numbers Logic gates Digital circuits process data using gates – Half and full adder Data storage – Electronic memory – Magnetic memory –
1 Processes and Threads Creation and Termination States Usage Implementations.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Chapter 11: Models of Computation
Categories of I/O Devices
I/O Systems.
Chapter 17 Linked Lists.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
OPERATING SYSTEM SUPPORT
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
1 © 2004, Cisco Systems, Inc. All rights reserved. CCNA 1 v3.1 Module 2 Networking Fundamentals.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Chapter 3: Processes.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Processes Management.
Processes Management.
Executional Architecture
Chapter 9: Subnetting IP Networks
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
XP New Perspectives on Browser and Basics Tutorial 1 1 Browser and Basics Tutorial 1.
Analyzing Genes and Genomes
Systems Analysis and Design in a Changing World, Fifth Edition
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
We will resume in: 25 Minutes.
© 2007 Cisco Systems, Inc. All rights reserved.Cisco Public 1 Addressing the Network – IPv4 Network Fundamentals – Chapter 6.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 13 Slide 1 Application architectures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Energy Generation in Mitochondria and Chlorplasts
Chapter 14 Introduction to Collections
Profile. 1.Open an Internet web browser and type into the web browser address bar. 2.You will see a web page similar to the one on.
Chapter 8 Improving the User Interface
TCP/IP Protocol Suite 1 Chapter 18 Upon completion you will be able to: Remote Login: Telnet Understand how TELNET works Understand the role of NVT in.
Fundamentals of Python: From First Programs Through Data Structures
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Threaded Programming in Python
Processes and threads.
Lecture 21 Concurrency Introduction
Threaded Programming in Python
Presentation transcript:

Chapter 15 Multithreading, Networks, and Client/Server Programming Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

Objectives Describe what threads do and explain the advantages of multithreading Explain how threads are manipulated in an application Code an algorithm to run as a thread Use conditions to solve a simple synchronization problem with threads 2 2

Objectives (continued) Use IP addresses, ports, and sockets to create a simple client/server application on a network Decompose a server application with threads to handle client requests efficiently Restructure existing applications for deployment as client/server applications on a network 3 3

Vocabulary client handler context switch IP address IP name IP number lock monitor multithreading parallel computing port ready queue server daemon 4 4

Vocabulary (continued) socket synchronized method Synchronization problem time slicing 5 5

Introduction Threads are processes that can run concurrently to solve a problem. Threads can be organized in a system of clients and servers. Example: A Web browser runs in a client thread and allows users to view Web pages sent by a Web server, which runs a server thread. Multithreading: running client and server threads concurrently on a computer or network. 6 6

Threads and Processes Algorithm: a computational process that runs to completion. A process consumes resources, such a CPU cycles and memory. When a program runs, the process associated with the program is not the only one running on your computer. Programs can also include concurrent processes. 7 7

Threads and Processes (continued) Time-sharing systems (1950s-60s): allowed several programs to run concurrently. Users logged in via remote terminals. The operating system created processes, and worked with the CPU and other resources. Today in the form of Web, e-mail, and print servers. 8 8

Threads and Processes (continued) Multiprocessing systems (1980s): a single user running several programs using a desktop. Forking: the ability of a program to start another program. Networked or distributed system (late 1980s, early 90s): CPUs linked by high-speed communication lines. 9 9

Threads and Processes (continued) Parallel computing: the discipline of building hardware architectures, operating systems, and specialized algorithms for running a program on a cluster of processors. Threads: Threads are used to describe processes. The JVM uses threads: the garbage collector runs as a separate thread from the main Java application. 10 10

Threads and Processes (continued) Threads (cont): In Java, a thread is an object. It can hold data, receive messages, be stored in data structures, and be passed as parameter to a method. Threads can also be executed as a process after the thread’s class implements a run method. Threads can enter various states. Ready queue is a data structure. CPU is a hardware resource. 11 11

Threads and Processes (continued) Threads (cont): States in the life of a thread 12 12

Threads and Processes (continued) Threads (cont): After a thread is created, it is inactive until someone runs its start method. Makes it ready for execution and puts it in a queue. Ready queue: threads ready for execution. After the thread starts to run, it can have problems that lead to being sent to the rear of the ready queue. Time slicing: the process of timing out. Pauses execution. 13 13

Threads and Processes (continued) Threads (cont): Sleep: puts to sleep for milliseconds. Block: thread waiting for an event, such as user input. Wait: voluntarily relinquish the CPU to wait for a condition to be true. Context switch: the process of saving or restoring a thread’s state. After the last instruction in the run method has executed, the thread dies as a process, but continues to be an object. 14 14

Threads and Processes (continued) Threads (cont): The most common way to create a thread is to define a class that extends Thread. The new class should include a run method that executes the algorithm in the new thread. Sleeping Threads: When a thread goes to sleep, the next thread can acquire the CPU. The size of the sleep interval determines the order in which threads are woken up. 15 15

Threads and Processes (continued) Sleeping Threads (cont): A run of the sleeping threads program 16 16

Threads and Processes (continued) Producer/Consumer Threads and Synchronization: Producer/consumer relationship: when threads interact by sharing data, like an assembly line. A producer must produce an item before a consumer consumes it. Each item must be consumed before the producer produces the next item. A consumer must consume each item just once. 17 17

Threads and Processes (continued) Producer/Consumer Threads and Synchronization (cont): Two runs of the producer/consumer program 18 18

Threads and Processes (continued) Producer/Consumer Threads and Synchronization (cont): Synchronization problems with the second run of the program: The consumer accessed the shared cell before the producer wrote the first datum. The producer writes two consecutive datum before the consumer accessed the cell again. The consumer accessed data 2 twice. The producer writes data 4 after the consumer is finished. 19 19

Threads and Processes (continued) Producer/Consumer Threads and Synchronization (cont): Producer and Consumer threads must synchronize their actions. The shared cell must be in one of two states: Writable or not writable. Conditions control the callers of setData (producer) and getData (consumer). Writable: getData must wait for consumer to write. Not writable: setData must wait until consumer reads datum. 20 20

Threads and Processes (continued) Producer/Consumer Threads and Synchronization (cont): Monitor: an object on which a process can obtain a lock. Lock: prevents another process from accessing data in the monitor until a condition is true. Synchronized method: the code runs as an indivisible unit. A second thread cannot execute method until first thread has completed executing the method. 21 21

Threads and Processes (continued) Producer/Consumer Threads and Synchronization (cont): The methods wait and notify suspend and resume the execution of the calling thread. Wait must be invoked within a try-catch statement. The producer/consumer problem can involve multiple producers and/or consumers. 22 22

Networks, Clients, and Servers The resources required to run client/server applications or process are: IP addresses, sockets, and threads. IP Addresses: IP number: ddd.ddd.ddd.ddd. IP name: for example, www.wwu.edu. Java uses InetAddress for IP addresses. 23 23

Networks, Clients, and Servers (continued) IP Addresses (cont): When developing a network application, the programmer can first try it on a local host, then change the IP address to deploy over the Internet. Ports, Servers, and Clients: Port: a channel through which several clients exchange data with the same or different servers. 24 24

Networks, Clients, and Servers (continued) A Day/Time Service: Ports and sockets 25 25

Networks, Clients, and Servers (continued) A Day/Time Service (cont): The sequence of events for the day/time service 26

Networks, Clients, and Servers (continued) A Day/Time Service (cont): An exception is thrown if there is a connection error, such as an unrecognized host. Making the Server Handle Several Clients: Most servers run an infinite command loop to take requests from an arbitrary number of clients. 27 27

Networks, Clients, and Servers (continued) Making the Server Handle Several Clients (cont): Using a while (true) loop can handle multiple requests, but creates problems. The main application in which the server runs cannot quit. The main application cannot do anything but run the server. Only one client can be handled at a time. 28 28

Networks, Clients, and Servers (continued) Using a Server Daemon and Client Handlers: Server daemon: a thread that listens indefinitely for client requests, but doesn’t handle them directly. Client handler: a thread that handles the client’s request. 29 29

Networks, Clients, and Servers (continued) Using a Server Daemon and Client Handlers (cont): Day/time server daemon with client handler 30 30

Networks, Clients, and Servers (continued) A Two-Way Chat Program: The client connects to the server, and the two programs engage in continuous communication until one of them (usually the client) quits. There are two distinct Java application for server and client. The server handles only one client at a time, so you do not need separate server daemon and client handler classes. The server program creates a socket with an IP address and port, then enters an infinite loop to accept and handle clients. 31 31

Networks, Clients, and Servers (continued) A Two-Way Chat Program (cont): When a client connects to the server, the server sends the client a greeting. The server then enters a second, nested loop. This loop engages the server and client in conversation. If the server receives a “bye” message from the client, the server displays the message, closes the socket, and breaks out of the nested loop. Otherwise, the server prints the client’s message and prompts the server’s user for a reply. 32 32

Networks, Clients, and Servers (continued) Setting Up Conversations for Others: The structure of a client/server program for clients and therapists. 33 33

Summary 34 34 In this chapter, you learned: Threads allow the work of a single program to be distributed among several computational processes. These processes may be run concurrently on the same computer or may collaborate by running on separate computers. A thread can have several states during its lifetime, such as new, ready, executing (in the CPU), sleeping, and waiting. The queue schedules the threads in first-come, first-served order. 34 34

Summary (continued) After a thread is started, it goes to the end of the ready queue to be scheduled for a turn in the CPU. A thread may give up the CPU when that thread times out, goes to sleep, waits on a condition, or finishes its run method. When a thread wakes up, times out, or is notified that it can stop waiting, it returns to the rear of the ready queue. 35 35

Summary (continued) Thread synchronization problems can occur when two or more threads share data. These threads can be synchronized by waiting on conditions that control access to the data. Each computer on a network has a unique IP address that allows other computers to locate it. An IP address contains an IP number, but can also be labeled with an IP name. 36 36

Summary (continued) Servers and clients can communicate on a network by means of sockets. A socket is created with a port number and an IP address of the server on the client’s computer and on the server’s computer. Clients and servers communicate by sending and receiving strings through their socket connections. A server can handle several clients concurrently by assigning each client request to a separate handler thread. 37 37