Session 5 Class and Object: Multi Threading

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

SESSION 5

Multi Threading
Class and Object
Introduction :

Multithreading in java is a process of executing multiple


threads simultaneously.
SESSION 5
Thread is basically a lightweight sub-process, a smallest
unit of processing. Multiprocessing and multithreading,
both are used toClass
achieveand Object
multitasking

A multithreaded program contains two or more parts that


can run concurrently. Each part of such a program is called
a thread, and each thread defines a separate path of
execution.
Multitasking:

Multitasking is a process of executing multiple


tasks simultaneously. We use5multitasking to
SESSION
utilize the CPU.

Multitasking Class
can be and Object
achieved by two ways:

1.Process-based Multitasking(Multiprocessing)

2.Thread-based Multitasking(Multithreading)
Process-based multitasking
A process is a program that is executing.
Thus, process-based multitasking is the feature that allows
your computer to runSESSION
two or more5programs concurrently.
In process-based multitasking, a program is the smallest
unit of code that can be dispatched by the scheduler.
Class and Object
Processes are heavyweight tasks that require their own
separate address spaces.

Inter-process communication is expensive and limited.


Context switching from one process to another is also
costly.
Thread-based multitasking

In a thread-based multitasking environment, the


thread is the smallest
SESSIONunit of
5 dispatchable code.
This means that a single program can perform
two or more tasks simultaneously.
Class and Object
For instance, a text editor can format text at the
same time that it is printing, as long as these two
actions are being performed by two separate
threads.
Thread-based multitasking-cont…
Multitasking threads require less overhead than
multitasking processes.
SESSION 5
Multitasking threads share the same address space and
cooperatively share the same heavyweight process.
Class and Object
Inter-thread communication is inexpensive.
Context switching from one thread to the next is low
cost.

Multithreaded multitasking is under the control of Java.


Benefits of Multithreading

Multithreading enables you to write very efficient


programs that make maximum use of the CPU,
SESSION 5
because idle time can be kept to a minimum.

This is especially
Classimportant for the interactive,
and Object
networked environment in which Java operates,
because idle time is common.

Multithreading lets you gain access to this idle time


and put it to good use.
Life cycle of a Thread(Thread States)

thread states are as follows:


SESSION 5
New
Runnable
Running Class and Object
Non-Runnable (Blocked)
Terminated
Thread states

SESSION 5

Class and Object


The Main Thread
When a Java program starts up, one thread begins
running immediately. This is usually called the main
thread of your program, because
SESSION 5 it is the one that is
executed when your program begins.

The main threadClass and Object


is important for two reasons:
It is the thread from which other “child” threads will be
spawned.

It must be the last thread to finish execution because it


performs various shutdown actions.
Creating a Thread

Create a thread by instantiating an object of


type Thread. SESSION 5

Java defines two ways in which this can be


accomplished:Class and Object

1. implement the Runnable interface.

2. extend the Thread class


Implementing Runnable

The easiest way to create a thread is to create a class


that implements theSESSION 5
Runnable interface.

Runnable abstracts a unit of executable code.


Class and Object
construct a thread on any object that implements
Runnable.
Using Runnable interface

To implement Runnable, a class need only implement a


single method called run( ), which is declared like this:
public void run( ) SESSION 5

Inside run( ), you will define the code that constitutes


the new thread.Class and Object
After you create a class that implements Runnable, you
will instantiate an object of type Thread from within
that class.
Practice and exercise programs

1. CurrentThreadDemo.java
SESSION
2. ThreadDemo.java 5
3. MultiThreadDemo.java
4. DemoJoin.java
Class and Object
SESSION 5
End of Session-23
Class and Object

You might also like