Multitasking: (1) by Extending Thread Class
Multitasking: (1) by Extending Thread Class
Multitasking: (1) by Extending Thread Class
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*
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 2 3 4
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
class Test
{
public static void main(String[] args)
{
MyThread t = new MyThread ();
t.start();
}
}
Output:
run method
Output:
Start Method
Main Method
#(9) After starting a thread if we are trying to restart the same thread then
we will get run time exception saying IlligalThread StateException