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.

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

Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
Multithreading The objectives of this chapter are:
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
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-
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.
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
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
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.
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.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Java Threads Representation and Management of Data on the Internet.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
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.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
1 Introduction to Threads Computers can perform many tasks concurrently – download a file, print a file, receive , etc. Sequential languages such.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading.
Multithreading in JAVA
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Scis.regis.edu ● CS-434: Object-Oriented Programming Using Java Week 8 Dr. Jesús Borrego Adjunct Faculty Regis University 1.
Java Thread and Memory Model
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.
Department of Computer Science and Software Engineering
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.
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.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
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.
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.
Java Thread Programming
Multithreading The objectives of this chapter are:
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Java Multithreading.
Concurrency, Processes and Threads
Multithreaded Programming in Java
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Multithreading.
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreading.
Multithreaded Programming
21 Threads.
9. Threads SE2811 Software Component Design
Multithreading in java.
9. Threads SE2811 Software Component Design
Concurrency, Processes and Threads
Multithreading The objectives of this chapter are:
9. Threads SE2811 Software Component Design
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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. Demonstrate the creation of Threads using Thread class. Demonstrate the creation of Threads using Runnable Interface. List frequently used methods of Thread class. : sleep(), join(), yield(), currentThread() etc. State the issues involved in Thread Priorities.

Basic Java : Multi Threading 3 Multiprocessing Many tasks done simultaneously. Program is a set of instructions and a “A process is a running instance of a program.” Here the CPU is shared between different processes. E.g. A MS-Word application & MS-Excel application windows opened simultaneously or in general any two applications running simultaneously is multiprocessing.

Basic Java : Multi Threading 4 Multi processing The OS provides you with a mechanism called CONTEXT SWITCHING which enables it to switch from process to process. Here what happens is a program’s entire context (including variables, global variables, functions etc..) are stored separately. Therefore if one program is running, it is as if other programs aren’t existing at all, because they reside in their own contexts.

Basic Java : Multi Threading 5 Multithreading Different tasks within a task done simultaneously. Extend the idea of multitasking by taking it one level lower. Individual programs appear to do multiple tasks at the same time. Each sub task is called Thread. Think of each thread running in a separate context - that is, each thread has its own CPU - with registers, memory & own code.

Basic Java : Multi Threading 6 Thread A thread is an entity within a process. You can say, “A thread within a process executes”. E.g.  Lets take a typical application like Word. There are various independent processes going on, like showing GUI, saving into file, typing in text.  In your java program main() is a thread & garbage collector is another thread.

Basic Java : Multi Threading 7 Process & Thread Each process has a complete set of its own variables It takes more overhead to launch new processes. Inter-process communication is slower & restrictive Threads may share the same data. It takes much less overhead to create & destroy threads Communication between threads is easier.

Basic Java : Multi Threading 8 Scheduling of Process / Thread Scheduling is decided by OS. Scheduling algorithm’s basically classified as:  Preemptive scheduling OS interrupts programs without consulting them E.g. : win 95, win NT  Non-preemptive scheduling programs interrupted only when they are ready to yield control. Eg : Sun Solaris, Win 3.1

Basic Java : Multi Threading 9 Multithreading In Context With Java Java supports multithreading. But Java is platform dependent as far as multithreading goes. JVM is running on top of OS. Thus, multithreading capability is totally dependent on what scheduling algorithm is followed by the underlying OS !! Thus inconsistent behavior is observed in multithreaded programs.

Basic Java : Multi Threading 10 Multithreading In Context With Java Java has a class Thread It’s a part of java.lang package Constructors of Thread class -  public Thread()  public Thread(Runnable target)  public Thread(ThreadGroup group, String name)  public Thread(Runnable target, String name)

Basic Java : Multi Threading 11 Life Cycle Of Thread Methods of Thread class -  run() : must be overridden; code should be added which executes in thread.  start() : brings the thread into ready to run condition. When CPU is available for this thread, invokes the run().  stop() : Forces the thread to stop executing. It’s a final method. (Deprecated)  sleep() : A static method, puts currently executing thread to sleep for specified no. of milliseconds.

Basic Java : Multi Threading 12 born ready running dead sleepingwaitingblockedsuspended or notifyAll start notify sleep suspend issue I/O request wait Sleep intervals expires resume quantum expiration dispatch (assign a processor) I/O completion completestop

Basic Java : Multi Threading 13 Runnable Interface Java can extend only one class. What if we want multithreading support for a class that is already derived from some other class?  Eg class MyFrame extends Frame Solution ? Runnable interface. Implementing this gives the ability to treat the new class as a Runnable object.

Basic Java : Multi Threading 14 Thread Priorities Thread priorities are used by thread scheduler  To decide when each thread should be allowed to run. Threads of equal priority compete equally for CPU. Whenever the thread scheduler has a chance to pick up a new thread, it generally picks up the highest-priority thread that is currently runnable.

Basic Java : Multi Threading 15 Setting Thread Priorities To set thread’s priority -  final void setPriority(int level) E.g. t1.setPriority(9); Value of level should be in the range of MAX_PRIORITY (10) & MIN_PRIORITY(1) Default priority is NORM_PRIORITY (defined at 5)

Basic Java : Multi Threading 16 Some Methods of the Thread Class join() yield() isAlive() sleep() currentThread() interrupt()

Basic Java : Multi Threading 17 join() This method waits for a thread to terminate. To do this, invoke join() on the thread object. E.g. Thread t=new ……(…); t.start(); t.join(); join() : waits forever for the thread to die. join(long msecs): waits msecs time. If thread doesn't die join() returns.

Basic Java : Multi Threading 18 yield() This methods causes currently executing thread to yield. If there are other runnable threads whose priority is at least as high as this thread, they will be scheduled next. Again everything is dependent on the operating system’s choice of scheduling.

Basic Java : Multi Threading 19 isAlive() returns true if thread has started & has not yet terminated. A thread is dead if  It dies a natural death because the run() method exits normally.  It dies abruptly because of an uncaught exception.

Basic Java : Multi Threading 20 currentThread() static native Thread currentThread() - returns a reference to current thread. Can be used in run() method to check which thread is currently executing E.g. : public void run(){ If (Thread.currentThread() == t1) // do something …… }