Concurrency…leading up to writing a web crawler. Web crawlers.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Multithreading The objectives of this chapter are:
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
3.5 Interprocess Communication
Chapter 11 Operating Systems
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
More Multithreaded Programming in Java David Meredith Aalborg University.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
11 Other Programming Aspects Dr. Miguel A. Labrador Department of Computer Science & Engineering
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
10/17/2015Vimal1 Threads By 10/17/2015Vimal2 Why use threads?? It is a powerful programming tool Computer users take it for granted.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
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,
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Concurrent Programming and Threads Threads Blocking a User Interface.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Process by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
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.
Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1.
Advanced Programming Concurrency and Threads Advanced Programming. All slides copyright: Chetan Arora.
Multithreading The objectives of this chapter are:
Chapter 4 – Thread Concepts
Multithreading / Concurrency
Processes and threads.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Chapter 4 – Thread Concepts
Lecture 21 Concurrency Introduction
Intro to Processes CSSE 332 Operating Systems
Lecture 7 Processes and Threads.
Multithreading Chapter 23.
Multithreading.
Java Based Techhnology
Multithreaded Programming
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Concurrency…leading up to writing a web crawler

Web crawlers

What is a thread?

First we need to know a little about concurrency.

Multi-tasking We frequently take for granted that our computers can do more than one thing at a time. ential/concurrency/index.htmlhttp://java.sun.com/docs/books/tutorial/ess ential/concurrency/index.html

Single Applications Even a single application is often expected to do more than one thing at a time. Example: Streaming video application must simultaneously: –Read the digital audio off the network –Decompress it –Manage playback

Another example Even Word should always be ready to: –Respond to keyboard events –Handle mouse events No matter how busy it is: –Reformatting text –Updating the display

Concurrency Software that can do such things is known as concurrent software.

Java and concurrency Java was designed from the beginning to support development of concurrent applications

Concurrent programming In concurrent programming, there are two basic units of execution: processes and threads. What do I mean by unit of execution? In Java, concurrent programming is mostly concerned with threads. However, processes are also important.

What is a program? How do you conceive of a program running on your machine? What are its properties?

Program A program is one process or two or more cooperating processes

What is a process? A self-contained execution environment –Code –Data (variables, etc.) –Available unallocated memory –Program counter

Time slicing A computer system normally has many active processes and threads. A single core can have only one thread actually executing at any given moment. Access to the processor is shared among processes and threads Accomplished through an OS feature called time slicing Small fractions of a second for each turn

Time slicing visually

Process states

Crawler threads are I/O bound

Multi-core machines Even machines with multiple execution cores use time slicing on the individual processors Each processor serves as the execution hub for many processes

Inter process communication Most OSs support Inter Process Communication (IPC) resources –Pipes –Sockets IPC also identifies communication between processes on different systems.

JVM – Single process Most implementations of the Java virtual machine run as a single process Java programs are frequently a single process with many threads

Cooperating processes Three processes each with one thread.

One process - cooperating threads One process with three threads.

Shared execution environment Shared –Code –Some data (variables, etc.) Unique –Program counter –Some data

Thread Objects Each thread is associated with an instance of the class Thread.Thread To directly control thread creation and management, simply instantiate Thread each time the application needs to initiate an asynchronous task.

Defining and starting a thread An application that creates an instance of Thread must provide the code that will run in that thread. Class should implement Runnable interfaceRunnable Method run() meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example.HelloRunnable Note the use of Thread.start to start the new thread.

Pausing execution with sleep Thread.sleep() causes the current thread to suspend execution for a specified period. Efficient means of making processor available to the other threads –Pacing –Waiting for another thread to perform some task. Specify sleep in milliseconds Sleeping threads can be woken up by an interrupt. The SleepMessages example uses sleep to print messages at four-second intervalsSleepMessages

SleepMessages example Notice that main() declares that it throws InterruptedException. Thread.sleep() throws this when another thread interrupts it. This application has not defined another thread to cause the interrupt. So we do not bother to catch the exception

Interrupts An interrupt signals a thread to stop what it is doing and do something else. The programmer decides exactly how a thread responds to an interrupt. Very common for the thread to terminate A thread sends an interrupt by invoking interrupt on the Thread object to be interrupted.interrupt The interrupted thread must support its own interruption. An interrupt signals a thread to stop what it is doing and do something else. The programmer decides exactly how a thread responds to an interrupt. Very common for the thread to terminate A thread sends an interrupt by invoking interrupt on the Thread object to be interrupted.interrupt The interrupted thread must support its own interruption.

Supporting interruption How does a thread support its own interruption? Typically, this is done by simply catching InterruptedException and responding appropriately. Can also use: –Thread.interrupted() –Thread.isInterrupted()

Catching InterruptedException

Using Thread.interrupted()

The interrupt status flag The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. Thread.interrupted checks, then clears this flag. (static method) Thread.isInterrupted just checks the interrupt status. (instance method) A method that exits by throwing an InterruptedException clears interrupt status.

Joins The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing, t.join(); causes the current thread to pause until t exits. Overloads of join allow the programmer to specify a waiting period.

SimpleThreads example SimpleThreads consists of two threads.SimpleThreads The first is the main thread that every Java application has. The main thread creates a new thread from the Runnable object, MessageLoop, and waits for it to finish. If the MessageLoop thread takes too long to finish, the main thread interrupts it. The MessageLoop thread prints out a series of messages. If interrupted before it has printed all its messages, the MessageLoop thread prints a message and exits.