Introduction to Sockets

Similar documents
Java for Interfaces and Networks

CSCD 330 Network Programming

CSCD 330 Network Programming

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Networking Basics. network communication.

Java for Interfaces and Networks (DT3029)

CSCD 330 Network Programming

CS September 2017

CSCD 330 Network Programming Spring 2018

Internet Technology 2/7/2013

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Distributed Systems Recitation 2. Tamim Jabban

Tommy Färnqvist, IDA, Linköping University

CSCD 330 Network Programming Winter 2019

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

BrickNet (cont d) Other Academic Projects

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

JAVA - NETWORKING (SOCKET PROGRAMMING)

Threads in Java (Deitel & Deitel)

Networking: IPv6, UDP and TCP. Network Programming in Java UDP and TCP

CSCD 330 Network Programming Spring 2018

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Multi-threaded Web Server (Assignment 1) Georgios Georgiadis

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005.

Byte and Character Streams. Reading and Writing Console input and output

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

Info 408 Distributed Applications programming 2 nd semester of 2017/2018 Credits: 5 Lecturer: Dr. Antoun Yaacoub

Pieter van den Hombergh Richard van den Ham. March 17, 2018

Principles, Models, and Applications for Distributed Systems M

Principles, Models, and Applications for Distributed Systems M

Advanced Java Programming. Networking

Object-Oriented Programming in Java

Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 URL

TCP connections. Fundamentals of Internet Connections Objectives. Connect to an Echo port. java.net.socket

CS 355. Computer Networking. Wei Lu, Ph.D., P.Eng.

Networking Code CSCI 201 Principles of Software Development

Socket 101 Excerpt from Network Programming

Principles of Software Construction. Introduction to networks and distributed systems School of Computer Science

Reintroduction to Concurrency

Communication in Distributed Systems: Sockets Programming. Operating Systems

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Multi-threading in Java. Jeff HUANG

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

Distributed Systems Recitation 2. Tamim Jabban

Lecture 3: Socket Programming

7. MULTITHREDED PROGRAMMING

Robotics and Autonomous Systems

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Unit III Rupali Sherekar 2017

THREADS AND MULTITASKING ROBOTS

ROBOTICS AND AUTONOMOUS SYSTEMS

Java Support for developing TCP Network Based Programs

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CMSC 330: Organization of Programming Languages

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Distributed Programming - Sockets

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Java Networking (sockets)

CMSC 132: Object-Oriented Programming II. Threads in Java

Assignment 1. Due date February 6, 2007 at 11pm. It must be submitted using submit command.

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Redes de Computadores (RCOMP)

31 Network Programming

Java Input/Output. 11 April 2013 OSU CSE 1

Chapter 4: Processes

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Chapter 11. Application-Layer Elements Ports

Synchronization in Concurrent Programming. Amit Gupta

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

User Space Multithreading. Computer Science, University of Warwick

CPSC 441 UDP Socket Programming. Department of Computer Science University of Calgary

CSCD 330 Network Programming

Java Programming Language Advance Feature

Redesde Computadores(RCOMP)

IT101. File Input and Output

CS18000: Programming I

CMSC 330: Organization of Programming Languages

Lab 2: File Input and Output

Writing a Protocol Handler

Java A.1 TCP/IP TCP. TCP_RO.java import java.net.*; import java.io.*;

THREADS AND CONCURRENCY

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Lab 1 : Java Sockets

ICOM 4015-Advanced Programming. Spring Instructor: Dr. Amir H. Chinaei. TAs: Hector Franqui, Jose Garcia, and Antonio Tapia. Reference: Big Java

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

CS360 Lecture 10 Multithreading

Synchronization synchronization.

CS18000: Programming I Final Exam 8 May 8am-10am Lilly Functional Abstraction. Functional Specification. CS18000: Programming I 4/28/2010

Chapter 2 Applications and

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

CS11 Java. Fall Lecture 7

URL Kullanımı Get URL

Transcription:

Introduction to Sockets Sockets in Java 07/02/2012 EPL 602 1

Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API o introduced in BSD4.1 UNIX, 1981 o explicitly created, used, released by apps o client/server paradigm o two types of transport service via socket API: unreliable datagram reliable, byte stream-oriented o Identified by protocol and local/remote address/port o Applications may refer to many sockets 07/02/2012 EPL 602 2

Socket-programming using TCP Socket: a door between application process and end-end-transport protocol (UDP or TCP) TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by operating system process socket TCP with buffers, variables internet process socket TCP with buffers, variables controlled by application developer controlled by operating system host or server host or server 07/02/2012 EPL 602 3

Socket types 07/02/2012 EPL 602 4

Socket o Socket abstraction Berkeley Sockets (traditional) Generic multiple families address representation independence) Uses existing I/O programming interface as much as possible o Sockets work with Unix I/O services just like files, pipes o Sockets have special needs: establishing a connection specifying communication endpoint addresses 07/02/2012 EPL 602 5

Network Programming o Historically, network programming has been errorprone, difficult, and complex. o The programmer had to know many details about the network and sometimes even the hardware. o You usually needed to understand the various layers of the networking protocol. o There were a lot of different functions in each different networking library concerned with connecting, packing, and unpacking blocks of information; shipping those blocks back and forth; and handshaking. 07/02/2012 EPL 602 6

Network Programming o It was a daunting task. o However, the concept of networking is not so difficult. You want to get some information from that machine over there and move it to this machine here, or vice versa. o Similar to reading and writing files the file exists on a remote machine the remote machine can decide exactly what it wants to do about the information you re requesting or sending. 07/02/2012 EPL 602 7

Java Network Programming o One of Java s great strengths is painless networking. o The programming model you use is that of a file. you actually wrap the network connection (a socket ) with stream objects, so you end up using the same method calls as you do with all other streams. o Java s built-in multithreading: handling multiple connections at once. 07/02/2012 EPL 602 8

Java Network Programming o Java uses the TCP/IP protocol The programmer doesn t see the details of TCP/IP. o Identifying a machine: //: WhoAmI.java // Finds out your network address when you're // connected to the Internet. import java.net.*; public class WhoAmI { public static void main(string[] args) throws Exception { if(args.length!= 1) { System.err.println( "Usage: WhoAmI MachineName"); System.exit(1); InetAddress a = InetAddress.getByName(args[0]); System.out.println(a); 07/02/2012 EPL 602 9

Sockets in Java o The socket is the software abstraction used to represent the terminals of a connection between two machines. o For a given connection there s a socket on each machine. In Java, you create a socket to make the connection to the other machine. Then you get an InputStream and OutputStream from the socket in order to be able to treat the connection as an IO stream object. 07/02/2012 EPL 602 10

Sockets in Java o There are two stream-based socket classes: a ServerSocket that a server uses to listen for incoming connections and a Socket that a client uses in order to initiate a connection. o Once a client makes a socket connection, the ServerSocket returns a corresponding server side Socket through which direct communications will take place. You have a true Socket to Socket connection. Use getinputstream( ) and getoutputstream( ) to produce the corresponding InputStream and OutputStream objects from each Socket. These must be wrapped inside buffers and formatting classes just like any other stream object. 07/02/2012 EPL 602 11

Sockets in Java o When you create a ServerSocket, you give it only a port number. o When you create a Socket you must give both the IP address and the port number where you re trying to connect. 07/02/2012 EPL 602 12

A simple server and client o All the server does is wait for a connection then uses the Socket produced by that connection to create an InputStream and OutputStream. o Then it reads from the InputStream and it echoes to the OutputStream until it receives the line END. o The client makes the connection to the server then creates an OutputStream. Lines of text are sent through the OutputStream. o The client creates an InputStream to hear what the server is saying. o Both the server and client use the same port number and the client uses the local loopback address to connect to the server. 07/02/2012 EPL 602 13

The Server //: JabberServer.java // Very simple server that just // echoes whatever the client sends. import java.io.*; import java.net.*; public class JabberServer { // Choose a port outside of the range 1-1024: public static final int PORT = 8080; public static void main(string[] args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Started: " + s); 07/02/2012 EPL 602 14

The Server (cont) try { // Blocks until a connection occurs: Socket socket = s.accept(); try { System.out.println( "Connection accepted: "+ socket); BufferedReader in=new BufferedReader( new InputStreamReader( socket.getinputstream())); // Output is automatically flushed // by PrintWriter: PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getoutputstream())),true); 07/02/2012 EPL 602 15

The Server (cont) while (true) { String str = in.readline(); if (str.equals("end")) break; System.out.println("Echoing: " + str); out.println(str); // Always close the two sockets... finally { System.out.println("closing..."); socket.close(); finally { s.close(); 07/02/2012 EPL 602 16

The Server (explanation) o See that the ServerSocket just needs a port number, not an IP address. o When you call accept( ), the method blocks until some client tries to connect to it. When a connection is made, accept( ) returns with a Socket object representing that connection. o The next part of the program looks just like opening files for reading and writing. Every time you write to out, its buffer must be flushed so the information goes out over the network. o The infinite while loop reads lines from the BufferedReader in and writes information to System.out and to the PrintWriter out. 07/02/2012 EPL 602 17

The Client //: JabberClient.java // Very simple client that just sends // lines to the server and reads lines // that the server sends. import java.net.*; import java.io.*; public class JabberClient { public static void main(string[] args) throws IOException { // Passing null to getbyname() produces the // special "Local Loopback" IP address, for // testing on one machine w/o a network: InetAddress addr = InetAddress.getByName(null); // Alternatively, you can use // the address or name: // InetAddress addr = InetAddress.getByName("127.0.0.1"); // InetAddress addr = InetAddress.getByName("localhost"); 07/02/2012 EPL 602 18

The Client (cont) System.out.println("addr = " + addr); Socket socket = new Socket(addr, JabberServer.PORT); // Guard everything in a try-finally to make // sure that the socket is closed: try { System.out.println("socket = " + socket); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getinputstream())); // Output is automatically flushed // by PrintWriter: PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getoutputstream())),true); 07/02/2012 EPL 602 19

The Client (cont) for(int i = 0; i < 10; i ++) { out.println("howdy " + i); String str = in.readline(); System.out.println(str); out.println("end"); finally { System.out.println("closing..."); socket.close(); 07/02/2012 EPL 602 20

The Client (explanation) o In main( ) you can see all three ways to produce the InetAddress of the local loopback IP address: using null, localhost, or the explicit reserved address 127.0.0.1. To connect to a machine across a network you substitute that machine s IP address. o Note that the Socket called socket is created with both the InetAddress and the port number. 07/02/2012 EPL 602 21

The Client (explanation) o Once the Socket object has been created, the process of turning it into a BufferedReader and PrintWriter is the same as in the server. o Note that the buffer must again be flushed (which happens automatically via the second argument to the PrintWriter constructor). If the buffer isn t flushed, the whole conversation will hang because the initial howdy will never get sent. o Each line that is sent back from the server is written to System.out to verify that everything is working correctly. 07/02/2012 EPL 602 22

Sockets in Java: Summary o There s actually a lot more to networking than can be covered in this introductory treatment. o Java networking also provides fairly extensive support for URLs, including protocol handlers for different types of content that can be discovered at an Internet site. o You ll also get the portability benefits of Java so you won t have to worry about the particular platform the server is hosted on. These and other features are fully and carefully described in Java Network Programming by Elliotte Rusty Harold (O Reilly, 1997). 07/02/2012 EPL 602 23

Introduction to Java Module 9: Threads 07/02/2012 Prepared by Chris Panayiotou for EPL 602 1

Introduction to Threads o Objects provide a way to divide a program up into independent sections. o Often, you also need to turn a program into separate, independently-running subtasks. o Each of these independent subtasks is called a thread. o You program as if each thread runs by itself and has the CPU to itself. 07/02/2012 EPL 602 2

Introduction to Threads o A process is a self-contained running program with its own address space. o A multitasking operating system is capable of running more than one process (program) at a time. o A thread is a single sequential flow of control within a process. o A single process can thus have multiple concurrently executing threads. 07/02/2012 EPL 602 3

Introduction to Threads o There are many possible uses for multithreading In general, you ll have some part of your program tied to a particular event or resource (and you don t want to hang up the rest of your program because of that). So you create a thread associated with that event or resource and let it run independently of the main program. A good example is a quit button you don t want to be forced to poll the quit button in every piece of code you write in your program and yet you want the quit button to be responsive, as if you were checking it regularly. In fact, one of the most immediately compelling reasons for multithreading is to produce a responsive user interface. 07/02/2012 EPL 602 4

Inheriting From Thread o The simplest way to create a thread is to inherit from class Thread, which has all the wiring necessary to create and run threads. o The most important method for Thread is run( ), which you must override to make the thread do your bidding. o Thus, run( ) is the code that will be executed simultaneously with the other threads in a program. 07/02/2012 EPL 602 5

Thread Example public class SimpleThread extends Thread { private int countdown = 5; private int threadnumber; private static int threadcount = 0; public SimpleThread() { threadnumber = ++threadcount; System.out.println("Making " + threadnumber); public void run() { while(true) { System.out.println("Thread " + threadnumber + "(" + countdown + ")"); if(--countdown == 0) return; public static void main(string[] args) { for(int i = 0; i < 5; i++) new SimpleThread().start(); System.out.println("All Threads Started"); 07/02/2012 EPL 602 6

Your First Thread o A run( ) method virtually always has some kind of loop that continues until the thread is no longer necessary. o In the previous example, in main( ) you can see a number of threads being created and run. o The special method that comes with the Thread class is start( ) (performs special initialization for the thread and then calls run( )). o So the steps are: the constructor is called to build the object. start( ) configures the thread and calls run( ). o If you don t call start( ) the thread will never be started. 07/02/2012 EPL 602 7

Runnable Interface o Java does not support multiple inheritance Instead, use interfaces Until now, inherited from class Thread, overrode run o Multithreading for an already derived class Implement interface Runnable (java.lang) New class objects "are" Runnable objects Override run method Controls thread, just as deriving from Thread class In fact, class Thread implements interface Runnable o Create new threads using Thread constructors Thread( runnableobject ) Thread( runnableobject, threadname ) 07/02/2012 EPL 602 8

Daemon Threads o A daemon thread is one that is supposed to provide a general service in the background as long as the program is running. o Not part of the essence of the program. Thus, when all of the non-daemon threads complete the program is terminated. o You can find out if a thread is a daemon by calling isdaemon( ), and you can turn the daemonhood of a thread on and off with setdaemon( ). o If a thread is a daemon, then any threads it creates will automatically be daemons. 07/02/2012 EPL 602 9

Example Daemon Threads class Daemon extends Thread { private static final int SIZE = 10; private Thread[] t = new Thread[SIZE]; public Daemon() { setdaemon(true); start(); public void run() { for(int i = 0; i < SIZE; i++) t[i] = new DaemonSpawn(i); for(int i = 0; i < SIZE; i++) System.out.println( "t[" + i + "].isdaemon() = " + t[i].isdaemon()); while(true) yield(); 07/02/2012 EPL 602 10

Example Daemon Threads class DaemonSpawn extends Thread { public DaemonSpawn(int i) { System.out.println("DaemonSpawn "+i+" started"); start(); public void run() { while(true) yield(); 07/02/2012 EPL 602 11

Example Daemon Threads public class Daemons { public static void main(string[] args) { Thread d = new Daemon(); System.out.println("d.isDaemon() = " + d.isdaemon()); // Allow the daemon threads to finish // their startup processes: BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Waiting for CR"); try { stdin.readline(); catch(ioexception e) { 07/02/2012 EPL 602 12

Example Daemon Threads o The Daemon thread sets its daemon flag to true and then spawns a bunch of other threads to show that they are also daemons. o Then it goes into an infinite loop that calls yield( ) to give up control to the other processes. o There s nothing to keep the program from terminating once main( ) finishes its job since there are nothing but daemon threads running. o So that you can see the results of starting all the daemon threads, System.in is set up to read so the program waits for a carriage return before terminating. 07/02/2012 EPL 602 13

Multithreading Issues o Sharing limited resources: You can think of a single-threaded program as one lonely entity moving around through your problem space and doing one thing at a time. You never have to think about the problem of two entities trying to use the same resource at the same time. With multithreading you now have the possibility of two or more threads trying to use the same limited resource at once. Colliding over a resource must be prevented or else you ll have two threads trying to access the same resource at the same time (e.g. print to the same printer, or adjust the same value, etc.) 07/02/2012 EPL 602 14

Multithreading Issues o A fundamental problem with using threads: You never know when a thread might be run. o Sometimes you don t care if a resource is being accessed at the same time you re using it. o Need some way to prevent two threads from accessing the same resource. (at least during critical periods) o Preventing this kind of collision is simply a matter of putting a lock on a resource when one thread is using it. o The first thread that accesses a resource locks it, and then the other threads cannot access that resource until it is unlocked. 07/02/2012 EPL 602 15

How Java Shares Resources o Java has built-in support to prevent collisions over one kind of resource: The memory in an object. Since you typically make the data elements of a class private and access that memory only through methods, you can prevent collisions by making a particular method synchronized. Only one thread at a time can call a synchronized method for a particular object. Here are simple synchronized methods: synchronized void f() { /*... */ synchronized void g(){ /*... */ 07/02/2012 EPL 602 16

How Java Shares Resources o Each object contains a single lock (also called a monitor) that is automatically part of the object (you don t have to write any special code). o When you call any synchronized method, that object is locked and no other synchronized method of that object can be called until the first one finishes and releases the lock. o In the example above, if f( ) is called for an object, g( ) cannot be called for the same object until f( ) is completed. 07/02/2012 EPL 602 17

How Java Shares Resources o There s a single lock that s shared by all the synchronized methods of a particular object. o There s also a single lock per class (as part of the Class object for the class), so that synchronized static methods can lock each other out from static data on a class-wide basis. o NOTE: If you want to guard some other resource from simultaneous access by multiple threads, you can do so by forcing access to that resource through synchronized methods. 07/02/2012 EPL 602 18

More on synchronized o We can remove the synchronized keyword from the entire method and instead put a synchronized block around the critical lines. o But what object should be used as the lock? The current object (this)! o Of course, all synchronization depends on programmer diligence: every piece of code that can access a shared resource must be wrapped in an appropriate synchronized block. 07/02/2012 EPL 602 19

Example public void run() { while (true) { synchronized(this) { t1.settext(integer.tostring(count1++)); t2.settext(integer.tostring(count2++)); try { sleep(500); catch (InterruptedException e){ 07/02/2012 EPL 602 20

Thread States o A thread can be in any one of four states: New: the thread object has been created but it hasn t been started yet so it cannot run. Runnable: This means that a thread can be run when the time-slicing mechanism has CPU cycles available for the thread. Dead: the normal way for a thread to die is by returning from its run( ) method. You can also call stop( ). Blocked: the thread could be run but there s something that prevents it. Until a thread reenters the runnable state it won t perform any operations. 07/02/2012 EPL 602 21

Becoming Blocked o A thread can become blocked for five reasons: You ve put the thread to sleep by calling sleep(milliseconds). You ve suspended the execution of the thread with suspend( ). It will not become runnable again until the thread gets the resume( ) message. You ve suspended the execution of the thread with wait( ). It will not become runnable again until the thread gets the notify( ) or notifyall( ) message. The thread is waiting for some IO to complete. The thread is trying to call a synchronized method on another object and that object s lock is not available. 07/02/2012 EPL 602 22

More on Blocking o You can also call yield( ) (a method of the Thread class) to voluntarily give up the CPU so that other threads can run. o However, the same thing happens if the scheduler decides that your thread has had enough time and jumps to another thread. o That is, nothing prevents the scheduler from re-starting your thread. o When a thread is blocked, there s some reason that it cannot continue running. 07/02/2012 EPL 602 23

Wait and Notify o The point with both sleep( ) and suspend( ) is that do not release the lock as they are called. o On the other hand, the method wait( ) does release the lock when it is called, which means that other synchronized methods in the thread object could be called during a wait( ). o You ll also see that there are two forms of wait( ). The first takes an argument in milliseconds that has the same meaning as in sleep( ) The second form takes no arguments 07/02/2012 EPL 602 24

Wait and Notify o You can put a wait( ) inside any synchronized method, regardless of whether there s any threading going on inside that particular class. o In fact, the only place you can call wait( ) is within a synchronized method or block. o If you call wait( ) or notify( ) within a method that s not synchronized, the program will compile, but when you run it you ll get an IllegalMonitorStateException with the somewhat non-intuitive message current thread not owner. 07/02/2012 EPL 602 25

Wait and Notify o You can call wait( ) or notify( ) only for your own lock. Again, you can compile code that tries to use the wrong lock, but it will produce the same IllegalMonitorStateException message as before. o You can t fool with someone else s lock, but you can ask another object to perform an operation that manipulates its own lock. o So one approach is to create a synchronized method that calls notify( ) for its own object. 07/02/2012 EPL 602 26

Deadlock o Because threads can become blocked and because objects can have synchronized methods that prevent threads from accessing that object until the synchronization lock is released, it s possible for one thread to get stuck waiting for another thread, which in turn waits for another thread, etc., until the chain leads back to a thread waiting on the first one. o This is called deadlock. The claim is that it doesn t happen that often, but when it happens to you it s frustrating to debug. o There is no language support to help prevent deadlock. 07/02/2012 EPL 602 27

Too Many Threads o You must watch to see that you don t have too many threads. o If you do, you must try to use techniques to balance the number of threads in your program. o If you see performance problems in a multithreaded program you now have a number of issues to examine: Do you have enough calls to sleep( ), yield( ), and/or wait( )? Are calls to sleep( ) long enough? Are you running too many threads? o Have you tried different platforms and JVMs? Issues like this are one reason that multithreaded programming is often considered an art. 07/02/2012 EPL 602 28

Summary o It is vital to learn when to use multithreading and when to avoid it. o The main drawbacks to multithreading are: Slowdown while waiting for shared resources Additional CPU overhead required to manage threads Unrewarded complexity, such as the silly idea of having a separate thread to update each element of an array Pathologies including starving, racing, and deadlock o An additional advantage to threads is that they are light execution context switches 07/02/2012 EPL 602 29

Summary o Threading is like stepping into an entirely new world and learning a whole new programming language, or at least a new set of language concepts. o One of the biggest difficulties with threads occurs because more than one thread might be sharing a resource. o There s a certain art to the application of threads. o A significant non-intuitive issue in threading is that, because of thread scheduling, you can typically make your applications run faster by inserting calls to sleep( ) inside run( ) s main loop. 07/02/2012 EPL 602 30