Lecture 9 Object Oriented Programming Using Java

Slides:



Advertisements
Similar presentations
1 Multithreaded Programming in Java. 2 Agenda Introduction Thread Applications Defining Threads Java Threads and States Examples.
Advertisements

Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Chapter 4 Threads Threads: Resource ownership and execution.
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.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Multithreading.
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.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
10/10/20151 MULTITHREADED PROGRAMMING. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
10/17/2015Vimal1 Threads By 10/17/2015Vimal2 Why use threads?? It is a powerful programming tool Computer users take it for granted.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
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.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
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.
Multithreading in JAVA
Packages Packages are containers for classes that are used to keep the class name space compartmentalized.. You can define classes inside a package that.
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.
SurfaceView.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
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.
1 Mouse Events See Figure See Example in Figure 11.28,
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
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 in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
6/11/2016 DEPT OF CSE 1 JAVA. 6/11/2016 DEPT OF CSE 2 MULTITHREADED PROGRAMMING.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Multithreading Lec 23.
Multi Threading.
Java Multithreading.
Multithreading.
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
Multithreading programming Pavan d.m.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Multithreading 2 Lec 24.
Java Based Techhnology
Processes and Threads.
Multithreaded Programming
21 Threads.
Java Thread.
Threads in Java James Brucker.
Multithreading in java.
Threads and Multithreading
Representation and Management of Data on the Internet
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Lecture 9 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar

Multithreading A multithreaded program contains two or more parts that can run concurrently. Each part of such program is called thread. A thread defines the path of execution. Each thread defines a separate path of execution. Multitasking is a specialized form of multithreading Kinds of threads

Introduction A process is actually a program in execution. In process based threads the unit is a program. In thread based threading the unit is a thread. Difference is Big picture vs. Details T1 Process based threads Thread based threads T2 T3 P1 P2 P1 P2 P3

Comparison of Thread Types Processes are heavy weight tasks than threads. IPC is expensive in processes. Context switching is expensive. Threads shares the same address space. Why multithreading? Java Thread Model Event loop with polling approach (single threaded app) In multithreaded applications this approach is eliminated

Life Cycle of a Thread Born Start Ready I/O Completes Quantum expiration Thread dispatch Issue I/O or Synchronized Statement Running Timeout Expires Wait Sleep Sleeping Blocked Waiting Complete

Juggling of a ball And Multithreading How Threads can be Implemented in Java Using Runnable Interface Using Thread Class

A simple program on Threads public class Main { public Main() { } public static void main(String[] args) { Thread t = Thread.currentThread(); System.out.println("Current Thread: " + t); t.setName("My Thread"); System.out.println("After name change: " + t); try{ for(int n=5; n > 0; n--) { System.out.println(n); Thread.sleep(1000); } } catch(InterruptedException ex){ } }} A simple program on Threads

Thread implementation using Runnable Interface public class Main implements Runnable { Thread t; public Main() { t = new Thread(this,"Testing Thread"); System.out.println("Child Thread: " + t); t.start(); } public void run(){ try{ for(int x=5; x > 0; x--){ System.out.println("Child Thread: " + x); Thread.sleep(500); } } catch(InterruptedException ex){ } }

public static void main(String[] args) { new Main(); try{ for(int i=5; i > 0; i--){ System.out.println("Main Thread: " + i); Thread.sleep(1000); } catch(InterruptedException ex){ System.out.println("Main Thread Interrupted"); System.out.println("Main Thread Exiting"); } }

Threads extending Threads public class Main extends Thread { Thread t; public Main() { t = new Thread(this,"Testing Thread"); System.out.println("Child Thread: " + t); t.start(); } public void run(){ try{ for(int x=5; x > 0; x--){ System.out.println("Child Thread: " + x); Thread.sleep(500); } } catch(InterruptedException ex){ System.out.println("Child Interrupted"); } System.out.println("Child Thread Exiting"); }

Threads extending Threads public class Main extends Thread { Thread t; public Main() { t = new Thread(this,"Testing Thread"); System.out.println("Child Thread: " + t); t.start(); } public void run(){ try{ for(int x=5; x > 0; x--){ System.out.println("Child Thread: " + x); Thread.sleep(500); } } catch(InterruptedException ex){ System.out.println("Child Interrupted"); } System.out.println("Child Thread Exiting"); }

Creating Multiple Threads public class Main extends Thread { String name; Thread t; public Main(String threadname) { name = threadname; t = new Thread(this,name); System.out.println("New Thread: " + t); t.start(); }

Creating Multiple Threads public void run(){ try{ for(int x=5; x > 0; x--){ System.out.println("name: " + x); Thread.sleep(500); } catch(InterruptedException ex){ System.out.println(name + " Interrupted"); System.out.println(name + " Thread Exiting");

Creating Multiple Threads public static void main(String[] args) { new Main("One"); new Main("Two"); new Main("Three"); try{ Thread.sleep(1000); } catch(InterruptedException ex){ System.out.println("Main Thread Interrupted"); System.out.println("Main Thread Exiting");