1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.

Slides:



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

Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading The objectives of this chapter are:
Software Engineering Oct-01 #11: All About Threads Phil Gross.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
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-
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
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.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Processes and Threads.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
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.
Java Threads Representation and Management of Data on the Internet.
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.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
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.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
Threading Eriq Muhammad Adams J
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
1 5.0 Garbage collector & Threads : Overview Introduction: In this module we discuss the merits and demerits of automated garbage collection over manual.
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.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
Multi-Threading in Java
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Multithreading The objectives of this chapter are: To understand the purpose of multithreading To describe Java's multithreading mechanism.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Doing Several Things at Once
Multithreading / Concurrency
Applied Operating System Concepts -
Chapter 5a: CPU Scheduling
Multithreaded Programming in Java
Multithreading in Java
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
ITEC324 Principle of CS III
Multithreading.
Java Based Techhnology
Multithreading.
21 Threads.
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 Threads in Java Jingdi Wang

2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of control within a single program Threads makes a program do more than one thing at a time

3 History of threads Early DOS and Windows systems were single tasking The concept of multithreading first appeared with time sharing systems High-performance graphical and networking applications promoted the growth of multithread operating systems

4 Threads in Java Java has built-in support for multithreading Java provides a unified multithreading API supported by all Java virtual machines on all platforms other languages (C, C++) use external thread package to do multithreading

5 Two ways to create threads Extend java.lang.Thread Implement the runnable interface In both cases must implement public void run(); which is the main logic of a thread

6 Extend java.lang.Thread public class Mythread extends Thread { public void run() { doWork(); } new Mythread().start();//the thread won’t run until you call start(), which is a method inherited from the parent class Thread

7 Implement the runnable interface public class MyRunner implements runnable{ public void run() { doWork(); } Thread myThread=new Thread(new MyRunner()); myThread.start(); //the thread won’t run until you call start(), which is a method inherited from the parent class Thread

8 Thread states - new The thread object is first created It is not yet executing When the thread's start() method is invoked, it changes to the runnable state

9 Thread states - runnable The thread is eligible for execution However, it is not necessarily running It can be allocated CPU time by the system when the CPU is available The CPU may not always be available

10 Thread states - blocked The thread is waiting for an I/O operation to complete Someone calls its sleep() method The thread calls its wait() method Someone calls its suspend() method The thread tries to obtained an object lock currently owned by another thread

11 Thread states - dead The thread’s run() method exits normally An uncaught exception terminates the thread’s run() method

12 Thread Priorities Priority values range from 1 (minimum) to 10 (maximum) Every thread has a priority When a thread is created, it inherits the priority of the thread that created it Priority can be set and changed

13 Thread scheduling Determines how runnable threads are allocated CPU time A thread-scheduling mechanism is either preemptive or non-preemptive A preemptive scheduler can be either time- sliced or non-time-sliced

14 Preemptive vs. non-preemptive A preemptive scheduler pauses a running thread to allow different threads to execute A non-preemptive scheduler never interrupts a running thread Instead, it relies on the running thread to yield control of CPU to other threads Threads may suffer from CPU starvation

15 Time slicing The scheduler allocates a period of time for which each thread can use the CPU When that amount of time has elapsed, the scheduler preempts the thread and switches to a different thread

16 Thread scheduling in Java Java threads are preemptive, but not guaranteed to be time sliced The current thread is preempted if a higher priority thread becomes runnable If an equal priority thread becomes runnable, there is no guarantee that it will be allocated CPU time until it becomes the highest priority runnable thread

17 Four kinds of threads programming Unrelated threads –simplest situation –threads do different things and don’t interact with each other Related but unsynchronized threads –threads work on different pieces of the same data structure –the threads don’t interact with each other

18 Four kinds of threads programming Mutually exclusive threads –threads share the same data –must avoid data inconsistency Communicating mutually exclusive threads –must notify other threads when data is produced or removed –must wait when the data buffer is empty or full

19 Pitfalls in multithreading Race condition –two or more threads update the same value simultaneously without synchronization –remedy: mutual exclusion Deadlock –remedy: careful programming

20 When to use threads  Lengthy processing: CPU-intensive calculation  Graphical user interface (GUI) display  Background processing: non-time critical tasks that need to execute continuously, eg. Garbage collection  Time-consuming I/O: avoid delay in unrelated parts of application