Threads CS 21b. Threads in Java zDefinition Õunit of program control that represents an execution sequence zJava supports multi-threaded execution * Programs.

Slides:



Advertisements
Similar presentations
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
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.
1 More on Threads b b A section of code executed independently of other threads written within a single program. b b Java threads can access global data;
1 L54 Networking (3). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Basic Socket Programming with Java. What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across.
1 Java - Threads A thread is an individual flow of control within a larger program. A program which is running more than one thread is said to be multithreaded.
Socket Programming.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
1 Multithreading. 2 Threads Program units that execute independently; multiple threads run “simultaneously” Virtual machine executes each thread for short.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L23 (Chapter 25) Networking.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
© 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.
TCP Socket Programming CPSC 441 Department of Computer Science University of Calgary.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
An Introduction to Internetworking. Algorithm for client-server communication with UDP (connectionless) A SERVER A CLIENT Create a server-socket (listener)and.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
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
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
1 Web Based Programming Section 8 James King 12 August 2003.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
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.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
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.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Graphics in Java CS 21b. The paint() Method Method in a visual component that specifies what that component looks like Particularly important for applets,
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Field Trip #25 Creating a Client/Server By Keith Lynn.
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.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
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 ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Object-Orientated Analysis, Design and Programming
Threads in Java Two ways to start a thread
MCA – 405 Elective –I (A) Java Programming & Technology
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.
Multiplexing/Demux.
Software Engineering and Architecture
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Clients and Servers 13-Sep-19.
Thread per client and Java NIO
Presentation transcript:

Threads CS 21b

Threads in Java zDefinition Õunit of program control that represents an execution sequence zJava supports multi-threaded execution * Programs we have written up to this point have been on a single thread of control

The Thread class zThread creation Õinstantiate a class that extends Thread Õinstantiate the class Thread but provide a run() method zThread execution Õlet t refer to a thread object Õt.start() causes the thread to execute its run() method “in the background”

Example 1: PrintThread zPrintThread class extends Thread zAttributes: name & timedelay zConstructor sets name and timedelay zrun method prints name twice but with a time delay in between the print statements Õuse Thread method sleep(timedelay)

run() Method for PrintThread public class PrintThread extends Thread {... public void run() { System.out.println(name); try { sleep(timedelay); } catch(Exception e) {} System.out.println(name); } … }

Using PrintThread PrintThread t1,t2,t3; t1 = new PrintThread("tic",5000); t2 = new PrintThread("tac",1000); t3 = new PrintThread("toe",3000); t1.start(); t2.start(); t3.start(); // expected output ? last output line should be tic

Example 2: MovingCircle zMovingCircle class extends Applet zAttributes: xpos & size (of circle) Õprovide initial values zpaint() method: draws the circle zThread created inside init() Õrun() method has a loop that continuously updates xpos & size and then repaints zA button executes the thread (t.start())

MovingCircle class (attributes & paint()) // sample animation using Threads public class MovingCircle extends Applet { int xpos = 5; // these variables will be updated int size = 10; // on a different thread... public void paint(Graphics g) { g.drawOval(xpos,50,size,size); } … }

MovingCircle class (thread code) t = new Thread() { public void run() { while (xpos < 80) { try {sleep(1000);} catch(Exception e) {} xpos +=5; size += 3; repaint(); // forces paint() to be re-executed } }}; // this statement placed inside the init() method

The Runnable Interface zRunnable is an interface that contains the run() method zOne of the constructors for Thread: public Thread(Runnable r) zCan create a thread by giving it an argument (object) that implements run() Õalternative to extending thread and then overriding run()

Thread Issues zDeadlock Õsome methods have been deprecated because of this possibility zSynchronization Õseveral threads running the same method and/or updating the same data

Networking CS 21b

Client/Server Computing zCommunication over the network often occurs between a client and a server zA server listens for connection requests and then responds to messages zA client establishes a connection to a server and then sends messages zTCP/IP: abstract layer that simplifies the above activities

Hosts, Ports, and Sockets zComputers on the network are (uniquely) specified by a host name or an IP address zCommunication between hosts occurs through their ports Õeach port allows several connections zSocket Õconnection handle that facilitates communication over the network

Networking in Java zjava.net package Õimport java.net.*; zMost important classes ÕSocket ÕServerSocket ÕURL (discussed earlier)

The Socket class zConstructor requires a host and port that the client intends to connect to zUseful Socket methods ÕInputStream getInputStream() ÕOutputStream getOutputStream() zUse file/stream interfaces to carry out the communication

The ServerSocket class zConstructor requires a port number that the server wishes to listen from zaccept() method returns a Socket object once a connection from a client has been established Õblocks (hangs) until the connection occurs

On the Server Program... zCreate a ServerSocket object Õspecify port zInvoke accept() on that object zObtain Socket object Õreturned by accept() zObtain I/O streams from the socket Õcarry out communication using these streams

On the Client Program... zCreate Socket object Õspecify host and port of server zObtain I/O streams from the socket Õcarry out communication using these streams * execute the server before the client

Allowing Multiple Connections to a Server zUse Threads zHave a loop that continuously calls accept() Õmain thread zCreate and start a thread whenever accept() returns a socket Õfacilitate communication on the socket using a separate thread