Presentation is loading. Please wait.

Presentation is loading. Please wait.

Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.

Similar presentations


Presentation on theme: "Collage of Information Technology University of Palestine Advanced programming MultiThreading 1."— Presentation transcript:

1 Collage of Information Technology University of Palestine Advanced programming MultiThreading 1

2 2 Outlines  Introduction  What threads are ?  Why they are useful?  The life cycle of a thread.  Thread priorities and scheduling.

3 3 Introduction  Perform one action at a time and perform it well is a nice work but that is usually difficult to do.  The human body performs a great variety of operations in parallel or, as we will say throughout this subject, concurrently.

4 4  Java makes concurrency available to the applications programmer through its APIs.  The programmer specifies that applications contain threads of execution.  This capability, called multithreading, gives the Java programmer powerful capabilities not available in the core C and C++ languages on which Java is based. Introduction

5 5

6 6

7 7  Writing multithreaded programs can be tricky.  Although the human mind can perform functions concurrently, people find it difficult to jump between parallel trains of thought.  To see why multithreading can be difficult to program and understand, try the following experiment: Open three books to page 1, and try reading the books concurrently.

8 8 What threads are ?  Conceptually, a thread is a flow of control within a program.  A thread is similar to the more familiar notion of a process, except that threads within the same application are much more closely related and share much of the same state.

9 9 Why they are useful?  Time  Process

10 10 The life cycle of a thread  At any given time, a thread is in one of five general states that encompass its lifecycle and activities. 1.New 2.Runnable 3.Blocked 4.Waiting 5.Timed_waiting 6.Terminated

11 11 The life cycle of a thread  NEW: The thread has been created but not yet started.

12 12

13 13  RUNNABLE:  The normal active state of a running thread, including the time when a thread is blocked in an I/O operation, like a read or write or network connection.  A thread in this state is considered to be executing its task.  At the operating-system level, Java's runnable state actually encompasses two separate states.  The process that an operating system uses to decide which thread to dispatch is known as thread scheduling and is dependent on thread priorities

14 14  BLOCKED:  It transitions to and from the runnable state.  When a runnable thread must wait to enter a synchronized statement, it transitions to the blocked state.  When the blocked thread enters the synchronized statement, it transitions to the runnable state. The life cycle of a thread

15 15  WAITING:  While the thread waits for another thread to perform a task.  Once in this state, a thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

16 16  TIMED_WAITING:  The thread is waiting for another thread via a call to wait( ) or join( ).  Another way to place a thread in the timed waiting state is to put the thread to sleep. A sleeping thread remains in the timed waiting state for a designated period of time (called a sleep interval) at which point it returns to the runnable state.  In the case of TIMED_WAITING, the call has a timeout.

17 17  TERMINATED: The thread has completed due to a return, an exception, or being stopped.

18 18 Thread priorities and scheduling  Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.  Java priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10).  Informally, threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads.  By default, every thread is given priority NORM_PRIORITY (a constant of 5). Each new thread inherits the priority of the thread that created it.

19 19 Round-robin  The job of an operating system's thread scheduler is to determine which thread runs next.  One simple implementation of the thread scheduler keeps the highest-priority thread running at all times and, if there is more than one highest-priority thread, ensures that all such threads execute for a quantum each in round- robin fashion.

20 20 Creating and Executing Threads  In J2SE 5.0, the preferred means of creating a multithreaded application is to implement the Runnable interface (package java.lang) and use built-in methods and classes to create Threads that execute the Runnables.  The Runnable interface declares a single method named run.  Runnables are executed by an object of a class that implements the Executor interface (package java.util.concurrent).

21 21 Creating and Executing Threads  This interface declares a single method named execute.  An Executor object typically creates and manages a group of threads called a thread pool.  These threads execute the Runnable objects passed to the execute method.  The Executor assigns each Runnable to one of the available threads in the thread pool.

22 22 Creating and Executing Threads  If there are no available threads in the thread pool, the Executor creates a new thread or waits for a thread to become available and assigns that thread the Runnable that was passed to method execute.  Depending on the Executor type, there may be a limit to the number of threads that can be created.  Interface ExecutorService (package java.util.concurrent) is a subinterface of Executor that declares a number of other methods for managing the life cycle of the Executor.

23 23 Creating and Executing Threads  If there are no available threads in the thread pool, the Executor creates a new thread or waits for a thread to become available and assigns that thread the Runnable that was passed to method execute.  Depending on the Executor type, there may be a limit to the number of threads that can be created.  Interface ExecutorService (package java.util.concurrent) is a subinterface of Executor that declares a number of other methods for managing the life cycle of the Executor.

24 24 Creating and Executing Threads  An object that implements the ExecutorService interface can be created using static methods declared in class Executors (package java.util.concurrent).  We use these interfaces and methods in the next application, which executes three threads.

25 25 Creating and Executing Threads

26 26 Creating and Executing Threads

27 27 Creating and Executing Threads

28 28 Creating and Executing Threads


Download ppt "Collage of Information Technology University of Palestine Advanced programming MultiThreading 1."

Similar presentations


Ads by Google