More About Threads.

Slides:



Advertisements
Similar presentations
Chapter 9 (Horstmann’s Book) Multithreading Hwajung Lee.
Advertisements

Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Java Programming: Advanced Topics
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Inter-Thread communication State dependency: Guarded Methods.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
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.
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.
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.
Advanced Programming 2004, based on LY Stefanus’s slides slide 8.1 Multithreading : Thread Scheduling ThreadGroup.
Multithreading in JAVA
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 More on Thread API.
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 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.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
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.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
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.
© Andy Wellings, 2004 Thread Priorities I  Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
Synchronization (Threads Accessing Shared Data). Contents I.The Bank Transfer Problem II.Doing a Simulation on the Bank Transfer Problem III.New Requirement:
Java Thread Programming
Internationalization The Number Format Problem
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Doing Several Things at Once
The Bouncing Ball Problem (Independent Threads)
Java Multithreading.
Multithreading.
Threads and Multithreading
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreaded Programming in Java
Multithreading Chapter 9.
Multithreading in Java
Threads Chate Patanothai.
ITEC324 Principle of CS III
Multithreading.
Cancellation.
Java Based Techhnology
Multithreading.
Threads and Multithreading
Threads and Multithreading
Java Concurrency 17-Jan-19.
21 Threads.
Threads and Multithreading
Java Concurrency.
Multithreading in java.
Threads and Multithreading
Threads and Multithreading
Java Concurrency.
Threads and Multithreading
Java Concurrency 29-May-19.
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

More About Threads

Contents Interrupting Threads Thread States Thread Properties

I. Interrupting Threads

The interrupt Method A thread terminates when its run method returns There is no way to force a thread to terminate. However, the interrupt method can be used to request termination of a thread When the interrupt method is called on a thread, the interrupted status of the thread is set. This is a Boolean flag that is present in every thread. Each thread should occasionally check whether it has been interrupted

To find out whether the interrupted status was set: while(. Thread To find out whether the interrupted status was set: while(!Thread.currentThread().isInterrupted() && more work to do) { do more work } When a thread is blocked, it cannot check the interrupted status !!! This is where the InterruptedException comes in.

public void run() { try { … while(. Thread. currentThread() public void run() { try { … while(!Thread.currentThread().isInterrupted() && more work to do) { do more work } } catch(InterruptedException e) { //Thread was interrupted during //sleep or wait } finally { .... } If your loop calls sleep method when the interrupted status is set, it doesn't sleep. Instead, it clears the status and throws an InterruptedException !!!

public void run() { try { … while(more work to do) { do more work Thread.sleep(delay); } } catch(InterruptedException e) { //Thread was interrupted during sleep } finally { .... }

II. Thread States

4 States New Runnable Blocked Dead

The New State When you create a thread with the new operator, the thread is in the new state

The Runnable State Once you invoke the start method, the thread is runnable A runnable thread may or may not actually be running. It is up to the operating system to give the thread time to run

The Blocked State A thread enters the blocked state when one of the following actions occurs: The thread goes to sleep by calling the sleep method The thread calls an operation that is blocking on input/output, that is, an operation that will not return to its caller until input and output operations are complete The thread tries to acquire a lock that is currently held by another thread The thread waits for a condition

The Dead State A thread is dead for one of two reasons: It dies a natural death because the run method exits normally It dies abruptly because an uncaught exception terminates the run method

III. Thread Properties

Thread Priorities By default, a thread inherits the priority of its parent thread, that is, the thread that started it Every thread has a priority value between MIN_PRIORITY (1) and MAX_PRIORITY (10). NORM_PRIORITY is defined as 5 Thread priorities are highly system dependent. Thus, it is best to treat thread priorities only as hints to the scheduler

Daemon Threads A daemon is simply a thread that has no other role in life than to serve others To turn a thread into a daemon thread: t.setDaemoon(true); Examples are timer threads that send regular "timer ticks" to other threads. When only daemon threads remain, the virtual machine exists. There is no point in keeping the program running if all remaining threads are daemons.

Reference Core Java, Volume I – Fundamentals, Eighth Edition, Chapter 14. Cay S. Horstmann and Gary Cornell. Prentice Hall, 2008