You are on page 1of 12

PRESENTATION ON

MULTITHREADING

INTRODUCTION

In todays world, computers have become an integral and


inseparable part of our lives.

Multithreaded programs extend the idea of multitasking by


taking it one level lower: individual programs will appear to
do multiple tasks at the same time.

Each task is usually called a threadwhich is short for


thread of control. Programs that can run more than one
thread at once are said to be multithreaded.

MULTI-THREADING .. ?
Multithreading is a conceptual programming concept where
a program (process) is divided into two or more subprograms
(process), which can be implemented at the same time in
parallel.
Each part of such a program is called a thread, and each
thread defines a separate path of execution.
Multithreading enables you to write in a way where multiple
activities can proceed concurrently in the same program.

Thread in java

A thread is a lightweight sub process, a smallest unit of


processing. It is a separate path of execution.
Threads are independent, if there occurs exception in one
thread, it doesn't affect other threads. It shares a common
memory area.
As shown in the above figure, thread is executed inside the
process. There can be multiple processes inside the OS and
one process can have multiple threads.

Creating a Thread

Java defines two ways in which this can be accomplished:


You can implement the Runnable interface.
You can extend the Thread class, itself.

1. BY EXTENDNG THREAD CLASS


The steps for creating a thread by using the first mechanism are:
1.Create a class by extending the Thread class and override the run()
method:
class MyThread extends Thread {
public void run()
// thread body of execution
2.Create a thread object:

}}

MyThread thr1 = new MyThread();

3. Start Execution of created thread:


thr1.start();

An example program illustrating creation and invocation of a thread


object is given below:

classMultiextendsThread{
publicvoidrun(){
System.out.println("threadisrunning...");
}
publicstaticvoidmain(Stringargs[]){
Multit1=newMulti();
t1.start();
}
}

2. By Implementing Runnable
The steps for creating a thread by using the second mechanism
are:
1.Create a class that implements the interface Runnable and
override run() method:
class MyThread implements Runnable {
...
public void run() {
// thread body of execution
}}
2. Creating Object:
MyThread myObject = new MyThread();
3. Creating Thread Object:
Thread thr1 = new Thread(myObject);
4. Start Execution:
thr1.start();

Example to Create a Thread using Runnable Interface :


classMulti3implementsRunnable{
publicvoidrun(){
System.out.println("threadisrunning...");
}

publicstaticvoidmain(Stringargs[]){
Multi3m1=newMulti3();
Threadt1=newThread(m1);
t1.start();

2 THREAD CLASS VERSUS RUNNABLE


INTERFACE

By extending the thread class, the derived class itself is


a thread object and it gains full control over the thread
life cycle. Implementing the Runnable interface does
not give developers any control over the thread itself,
as it simply defines the unit of work that will be
executed in a thread.

when extending the Thread class, the derived class


cannot extend any other base classes because Java
only allows single inheritance. By implementing the
Runnable interface, the class can still extend other base
classes if necessary.

C O N C LU S I O N
Multithreading is an important feature to java
programing. It has various advantages which include:
Responsiveness
Resource sharing
Economy
Utilization of multiprocessor architectures
It doesn't block the user because threads are
independent and you can perform multiple operations
at same time.
You can perform many operations together so it saves
time.

Multithreading even include some of the


disadvantages , which include:
More-complicated code
Deadlocks (very hard to debug logical program errors)

u
o
Y
k
n
a
Th

You might also like