Network Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

IT 4110 - Network Programming

Lecture 08
Threads

Ransara Wijethunga
Tech. Lead
Millennium IT
2016 – 09 - 24
References / Recommended Textbooks

 Java Network Programming – 3rd Ed.


◦ Elliotte Rusty Harold

 Unix Network Programming – Vol2 2nd Ed.


◦ W. Richard Stevens

2
Overview
 This lecture provides the introductory knowledge
for usage of threads in industry standard
applications. Then, this lectures covers inter
process/thread communication as well.

3
Introduction
 Most non-trivial programs involve some
form of inter-process communication (IPC).
 This is a natural effect of the design principle
that the better approach is to design an
application
◦ as a group of small pieces that communicate with
each other,
◦ instead of designing one huge monolithic
program.

4
How Applications are Designed…
 Historically, applications have been built in the following
ways:
◦ One huge monolithic program that does everything. The
various pieces of the program can be implemented as functions that
exchange information as function parameters, function return values,
and global variables.

◦ Multiple programs that communicate with each other


using some form of IPC. Many of the standard Unix tools were
designed in this fashion, using shell pipelines (a form of IPC) to pass
information from one program to the next.

◦ One program comprised of multiple threads that


communicate with each other using some type of IPC. The term IPC
describes this communication even though it is between threads and
not between processes. 5
FTP in 1990s…
% ftp eunl.java.sun.com
Connected to eunl.javasoft.com.
220 softwarenl FTP server (wu-2.4.2-academ[BETA-
16]+opie-2.32(1) 981105) ready.
Name (eunl.java.sun.com:elharo): anonymous
530-
530- Server is busy. Please try again later or try one of
530- our other ftp servers at ftp.java.sun.com.
530- Thank you.
530-
530 User anonymous access denied.
Login failed.

6
FTP in 1990s… (cont.)
 The problem was that both the FTP servers bundled
with most Unix boxes and the third-party FTP servers,
forked a new process for each connection.

 100 simultaneous users meant 100 additional processes


to handle. Since processes are fairly heavyweight items,
too many could rapidly bring a server to its knees.

 The problem wasn‟t that the machines weren‟t powerful


enough or the network fast enough; it was that the FTP
servers were poorly implemented.

 Many more simultaneous users could be served if a new


process wasn‟t needed for each connection. 7
Solutions…
 The first is to reuse processes rather than spawning
new ones.
 When the server starts up, a fixed number of processes
(say, 300) are spawned to handle requests.

 The spawned processes are in wait (suspended) state in


a queue.
 Incoming requests are placed in another queue.

 When a new request comes, its added to the request


queue and one process from the process queue is
signaled.
 The waked-up process removes one request from the
queue, services the request, then returns to the queue. 8
Solutions… (cont.)
 The second solution to this problem is to use
lightweight threads to handle connections instead of
heavyweight processes.

 Whereas each separate process has its own block of


memory, threads are easier on resources because they
share memory.
 Using threads instead of processes can buy you another
factor of three in server performance.

 By combining this with a pool of reusable threads (as


opposed to a pool of reusable processes), your server
can run nine times faster, on the same hardware and
network connection.
9
Process / Thread Caching
 Why pre-spawned processes or pre-forked
add performance?

◦ No cost of thread/process creation at the time of


request serving.
◦ Time for serving a request is low. No of requests
per second is higher.
◦ Threads are light weighted, lesser stress to the
machine/CPU.

◦ By using a thread pool instead of spawning new


threads for each connection, a server can use
fewer than a hundred threads to handle
thousands of connections per minute.
10
Processes & Threads

11
Processes
 A process has a self-contained execution environment.
 A process has a complete, private set of basic run-time
resources.
 Each process has its own memory space.

 Processes are often seen as synonymous with programs or


applications.
 However, what the user sees as a single application may in
fact be a set of cooperating processes.
 To facilitate communication between processes, most
operating systems support Inter Process Communication
(IPC) resources, such as pipes and sockets.
12
Java Process Builder
 IPC is used not just for communication between
processes on the same system, but processes on
different systems.

 Most implementations of the Java virtual machine run as


a single process.
 A Java application can create additional processes using
a ProcessBuilder object.
 Multiprocess applications using above are not coded in
this lecture.

13
Threads
 Threads are sometimes called lightweight processes.
 Both processes and threads provide an execution
environment, but creating a new thread requires fewer
resources than creating a new process.

 Threads exist within a process; every process has at


least one thread (ie: the main thread).
 Threads share the process's resources, including
memory and open files.
 This makes for efficient, but potentially problematic
communication.
14
Coding Threads in Java
 An application that creates an instance of
Thread must provide the code that will
run in that thread.
 There are two ways to do this.
1. Providing a “Runnable” object
2. Subclass “Thread”

15
1. Providing a „Runnable‟ Object
 The Runnable interface defines a single
method, run, meant to contain the code
executed in the thread.
 The Runnable object is passed to the
Thread constructor

 Note: The application invoke Thread.start() in


order to start the new thread.
16
17
2. Subclass „Thread‟
 The Thread class itself implements
Runnable, though its run method does
nothing.
 An application can subclass Thread,
providing its own implementation of run.

 Note: The application invoke Thread.start()


in order to start the new thread.
18
19
DO IT YOUR OWN !!!

Remember our first server ?


our Daytime Server can
accept multiple requests, but can
process only one.

Apply Threads
( by implementing runnable)
20
Our Previous Code

21
Questions For Coming Week:
 Java Process Builder class.
 Thread management with Java Executor.
◦ http://docs.oracle.com/javase/tutorial/essential/concurrency
/highlevel.html
 CMPT300 Videos : 7. SFU CMPT 300: Processes &
Threads in Operating System
◦ http://www.youtube.com/watch?v=gif8znTA1Jw

 UCBerkeley : Computer Science 162 - Lecture 3


◦ http://www.youtube.com/watch?v=1I8Rg7065PE

 Thread Synchronization
22
Questions

 Try to solve by your self first !


“Do Google” or “Bing It” !!

 If failed, email me
◦ ransaraw@gmail.com

23

You might also like