Multitasking: (1) by Extending Thread Class

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Multitasking

Executing several tasks simultaneously is the concept of multitasking


There are two types of multitasking
(1) Process based multitasking:​ Executing several tasks simultaneously where each task is the separate
independent program (process) is called process based multitasking
eg) while typing a java program in the editor we can listen to audio songs from the same system at the
same time we can download a file from the net all these tasks will be executed simultaneously and
independent of each other hence it is process based multitasking
Process based multitasking is best suitable at os level
(2) Thread based multitasking: ​Executing several tasks simultaneously where each task is a separate
part of the same program is called thread based multitasking and each independent part is called a
thread
thread based multitasking is best suitable at programmatic level

Note: *​Whether it is process based or thread based multitasking the main objective of multitasking is to
reduce response time of the system and to improve performance​*

The main important applications areas of multithreading are


1) to develop multimedia graphics
2) to develop animations
3) to develop video-games
4) to develop web servers and applications server etc.

When compared with old languages developing multi threaded application in java is very easy because
java provides inbuilt support for multi threading with rich api (Thread , Runnable , Thread Group...)

#Defining a Thread
We can define a thread in the following two ways
1) By extending thread class : 2) By implementing runnable interface

(1) By extending thread class

class MyThread extends Thread public class ThreadDemo


{ {
public void run() public static void main(String[] args)
{ {
for(i=0;i<10;i++) // job of thread //At this time only one thread = main Thread
{ MyThread t = new mytherad(); // Thread Instantiation
systom.out.println(“child thread”); t.start(); // Starting of a thread
}// Executing by child thread //At this time two thread = main and child thread
} for (int i=10; i<8; i++)
} {
systom.out.println(“Main thread”);
} // Executing by main thread
}
}
#Case(1) Thread Scheduler 
(1)It is the part of JVM
(2)It is responsible Schedule threads that is if multiple threads are waiting
to get chance of execution than in which order thread will be executed is
decided by Thread Scheduler
(3) we can't expect exact algorithm followed by thread scheduler it is varied
from JVM to JVM hence we can't expect thread execution order and exact output
Hence whenever situation comes to multi threading there is no guarantee for
exact output but we can provide several possible outputs

The following are various possible outputs for above program

1 2 3 4

Main Thread Child Thread Main Thread Child Thread

Main Thread Child Thread Child Thread Main Thread

Main Thread Child Thread Main Thread Child Thread

Main Thread Child Thread Child Thread Main Thread

Main Thread Child Thread Main Thread Child Thread

Child Thread Main Thread Child Thread Main Thread

Child Thread Main Thread Main Thread Child Thread

Child Thread Main Thread Child Thread Main Thread

Child Thread Main Thread Main Thread Child Thread

Child Thread Main Thread Child Thread Main Thread


 
#Case(2) Difference between t.start(); and t.run()​;
In the cash of t.start() a new thread will be created which is responsible for
the execution of run method but
In the cash of t.run() a new thread wont be created and run method will be
executed just like a normal method call by main Thread

Hence in the above program if we replace t.start() with t.run() then the
output is
Child thread 10 times followed by Main Thread 10 times
This total output is produced by only main thread

#Case(3): Importance of thread class Start() method 


Thread class start method is responsible to register the thread with thread
scheduler and all other mandatory activity hence without executing thread class
start method there is no chance of starting a new thread in java due to this
thread class start method is considered as heart of multithreading
Start()
{ 1) Resister this thread with thread Scheduler
2) Perform all other mandatory activity
3) Invoke run();
}

#Case(4): Overloading of run method 


Overloading of run method is always possiible but thread class start method
can invoke no arg run method the other overloaded method we have to call
explicitly like a normal method call
Public class MyThread extends Thread
{
public void run()
{
System.out.println("run method");
}
public void run(int i)
{
System.out.println("int run method");
}
}

class Test
{
public static void main(String[] args)
{
MyThread t = new MyThread ();
t.start();
}
}
Output: 

run method

#Case(5): If we are not overriding run method  


If u r not overriding run method then thread clas
S run method will be executed which has empty implementation hence we wont get
any output

#Case(6):​ ​Overriding of start method 


If we override start method then our start method will be executed just like a
normal method call and new thread wont be created

class MyThread extends Thread {


public void start()
{
System.out.println("Start Method");
}
public void run()
{
System.out.println("Run Method");
}
}
class test {
public static void main(String[] args)
{
Mythread thread = new Mythread();
thread.start();
System.out.println("Main Method");
}
}

Output: 

Start Method
Main Method

class MyThread extends Thread {


public void start()
{
super.start();
System.out.println("Start Method");
}
public void run()
{
System.out.println("Run Method");
}
}
class test {
public static void main(String[] args)
{
Mythread thread = new Mythread();
thread.start();
System.out.println("Main Method");
}
}
Output:

Possibility :1 Possibility :2 Possibility :3

Run Method Start Method Start Method

Start Method Main Method Run Method

Main Method Run Method Main Method


#Life Cycle of a thread

#(9) After starting a thread if we are trying to restart the same thread then
we will get run time exception saying IlligalThread StateException

2) By implementing runnable interface

You might also like