Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Concurrency 101 Shared state. Part 1: General Concepts 2.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
Fundamentals of Python: From First Programs Through Data Structures
More Multithreaded Programming in Java David Meredith Aalborg University.
Multithreading.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Ryerson University CPS Distributing Computing with Java.
Internet Software Development More stuff on Threads Paul Krause.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 Web Based Programming Section 8 James King 12 August 2003.
1 Threads  Sequential Execution: Here statements are executed one after the other.They consider only a single thread of execution, where thread is an.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
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.
Internet Software Development Controlling Threads Paul J Krause.
Threading Eriq Muhammad Adams J
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Java Thread and Memory Model
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
© Andy Wellings, 2004 Thread Priorities I  Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Distributed and Parallel Processing George Wells.
CSCD 330 Network Programming
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Threaded Programming in Python
Multi Threading.
Lecture 21 Concurrency Introduction
Threads Chate Patanothai.
Multithreading Chapter 23.
Clients and Servers 19-Nov-18.
Synchronization Lecture 23 – Fall 2017.
Clients and Servers 1-Dec-18.
Multithreading.
Race Conditions & Synchronization
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Concurrency: Mutual Exclusion and Process Synchronization
Threaded Programming in Python
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
CSCD 330 Network Programming
Slide design: Dr. Mark L. Hornick
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Threads ICW Lecture 10 Tom Chothia

Last Time XML JDOM XPATH

This Lecture URLs A reminder of Sockets. Threads: running processes at the same time on the same computer. They can be tricky to use.

Uniform Resource Locators Many Internet systems use URLs, e.g. This URL refers to the file “index.html” on the host “ which should be accessed using http. Protocol Host FilePath

URLs aren't just for HTTP ftp://ftp.funet.fi/pub/standards/RFC/rfc 959.txt ftp://ftp.funet.fi/pub/standards/RFC/rfc 959.txt rmi:// /process svn+ssh://svn.cwi.nl/projects

More complex URLs ax Port number Username Quiry

URL syntax The most general URL has the syntax: /filepathname?query_string#anchor All parts are options!

URLs in Java The java.net.URL class does URLs in Java e.g.: URL myURL = new URL (“ myURL.getHost(); myURL.getPort();

URLs in Java The URL knows the protocol therefore it can connect for you: InputStream = myURL.openStream(); More info at: net/URL.html

Sockets A reminder: Code from Lecture 6. Only one connection at a time. For multiple connections you need to use Threads.

Threads A thread is a Java process that runs at the same time as other processes. Use threads for: Speed on Multi-process machines. When you want to handle lots of requests at the same time. When it's more “natural”.

Example: Chat Server Servers handle the requests from the clients concurrently: we can use Threads. Define the object that handles the request as “runnable” and define a run method. Declare it as a new Thread. Call the start method on it.

Threads and Speed Threads only speed up your program if your computer has more than one processor. To find the number of processors: Runtime runtime = Runtime.getRuntime(); int noOfProcessors = runtime.availableProcessors();

Can “value” ever go below 0? Class Counter { private static int value = 1000; public static void less (int i) { if (i<value) {value=value – i; } }

What about now? Class Counter extends Thread { private static int value = 1000; public static void less (int i) { if (i<value) {value=value – i; } }

Synchronisation Threads run concurrently Threads share the same data space Threads can interact in unexpected ways e.g. The state of object can only be kept consistent by guarding against such interactions.

What must we do to cope with this? Parts of the program must declare that they must have exclusive access to an object whilst performing an operation synchronised is used to mark methods or statements as needing exclusive access This implements a lock on the object

Synchronised methods Use synchronized to declare critical method Only one thread can executing a synchronised method on an object at a time: –To execute a synchronised method the thread must obtain the lock on that object –Whilst it holds the lock, no other thread can execute a synchronised method on that object - it is blocked

What about now? Class Counter extends Thread { private static int value = 1000; public static synchronised void less (int i) { if (i<value) {value=value – i; } }

Synchronised Statements Individual statements can also be synchronised. Syntax: synchronized (object) {statement} e.g:...; synchronized (foo) { i=foo.getSize(); foo.setSize(i*2); }...

Deadlock Deadlock describes a state where the program cannot proceed because of interactions between locks held by threads. For instance: –A is blocked waiting for a lock held by B –B is blocked waiting for a lock held by A The interactions can be indirect

Synchronisation/locks It is sometimes necessary to restrict access to objects by multiple threads - to maintain consistency As a rule you should minimise the use of locks Problems can arise: –not making code thread-safe –deadlocks Such problems are hard to debug

Sleep A thread can choice to stop running for a time using the sleep method: Thread.sleep(1000) Pauses the current Thread for about 1 sec.

Wait, Notify The wait() method cause the process to stop and give up its locks. The process remains paused until another process calls notify() on the same object. Notifyall() restarts all waiting processes.

Communication Between Threads Threads don't return values. Threads often run independenly to offer a service. However a thread can write to e.g. a buffer, for other threads to read from.

Main Program Creates a Buffer Main Program Results Buffer

Main can read from this buffer Main Program Results Buffer

Main creates a thread and passes it the buffer Main Program Results Buffer Thread 1

Main creates and starts another thread Main Program Results Buffer Thread 1 Thread 2

The threads write to the buffer, from which Main can read Main Program Results Buffer Thread 1 Thread 2

Conclusion Threads let you run concurrent processes. –Good for multi-core machines. –Good for services. Extend Thread/Implenement Runable Define a run() method.

Next Time Javascript