1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR

Slides:



Advertisements
Similar presentations
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Advertisements

1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
Concurrency…leading up to writing a web crawler. Web crawlers.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Multithreading. 2 Threads Program units that execute independently; multiple threads run “simultaneously” Virtual machine executes each thread for short.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
ThreadThread Thread Basics Thread Synchronization Animations.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
Java Threads CS Introduction to Operating Systems.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Multithreading.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
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.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
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.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
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.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
1 Dr.A.Srinivas PES Institute of Technology Bangalore, India
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
Multithreaded applets
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreading Chapter 9.
Threads Chate Patanothai.
Multithreading.
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreading.
برنامه‌نویسی چندنخی Multi-Thread Programming
Java Thread.
Threads and Multithreading
Representation and Management of Data on the Internet
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR

2 Concurrent Programming Running tasks in parallel Multiple processes Multiple threads in one process? Advantage: speed if you can decompose a task into parallel independent tasks Examples Webserver: multiple requests to cater to. Web browser: multiple objects loading simultaneously Assignment 4: quickly cracking a hash of a password? Encoding multiple blocks simultaneously Almost everything today can be broken into parallel tasks.

3 Threads and running processes

4 Creating Threads: extend the Thread class Thread is a basic class which provides two methods void start() Creates a new thread and makes it runnable void run() A new Thread begins its life inside this method public class A extends Thread { public A() { … constructor…} public void run() {…method that will run in the thread….} }

5 Lets take a concrete example in eclipse Application: Provide parallel addition Input: array of integers a[1,…,k] Divide array into segments and in parallel add segments of the array For example: Thread 1: add a[1:k/2] Theads 2: add a[k/2:k] Add both of them together

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

Thread Creation Diagram Thread t = new BThread(); t.start(); doMoreStuff(); Object A BThread() { } void start() { // create thread } void run() { doSomething(); } Object BThread (extends Thread)

15 Another way of creating Threads: Runnable interface A helper to the thread object Your class implements the Runnable interface The Thread object’s start() method calls the Runnable object’s run() method Allows threads to run inside any object, regardless of inheritance

16 Example of using the Runnable interface public class MyRunnable implements Runnable { String name; public MyRunnable(String name) { this.name = name; } public void run() { for(int i; i < 10; i++) { System.out.println(i + “ “ + name()); try { sleep((long)(Math.random() * 1000)); } catch(InterruptedException e) {} } public class ThreadTest { public static void main(String[] args) { for(int i = 0; i < args.length; i++) { Thread t = new Thread(new MyRunnable(args[i]), args[i]); t.start(); }

Blocking Threads When reading from a stream, if input is not available, the thread will block Thread is suspended (“blocked”) until I/O is available Allows other threads to automatically activate When I/O available, thread wakes back up again – Becomes “runnable” – Not to be confused with the Runnable interface

Thread Scheduling In general, the runnable thread with the highest priority is active (running) Java is priority-preemptive – If a high-priority thread wakes up, and a low-priority thread is running – Then the high-priority thread gets to run immediately Allows on-demand processing – Efficient use of CPU

Thread Starvation If a high priority thread never blocks Then all other threads will starve Must be clever about thread priority

Thread Priorities: General Strategies Threads that have more to do should get lower priority Counterintuitive Cut to head of line for short tasks Give your I/O-bound threads high priority – Wake up, immediately process data, go back to waiting for I/O

Thread interruption Threads execution exits when the run() method returns Or if it throws an exception that is not handled in the run() method What if you want to interrupt a running Thread? Thread.interrupt() --- call interrupts a Thread – Sets a interrupt flag for the Thread object! – How does a Thread check whether the flag is checked? – Thread.currentThread.isInterrupted()?

Example while (!Thread.currentThread().isInterrupted()) { …do something ….} What if the Thread is sleeping or blocked? Solution: catch InterruptedException? try { while(!Thread.currentThread.isInterrupted()) {..do something…} catch(InterruptedException e) { //thread interrupted during sleep or wait} When interrupted, interrupt flag is set and the Thread is woken up!

Race Conditions Two threads are simultaneously modifying a single object Both threads “race” to store their value In the end, the last one there “wins the race” (Actually, both lose)

Race Condition Example Lets take an example in eclipse to illustrate this

Thread Lifecycle Born Blocked Runnable Dead stop() start() stop() Active block on I/O I/O available JVM sleep(500) wake up suspend() resume() wait notify