Multitasking :
Executing several tasks
simultaneously is the concept of multitasking.
There are two types of multitasking
1.Process
based multitasking
2.Thread
based multitasking
Process based
multitasking:
Executing several tasks
simultaneously where each task is a separate independent process such type of
multitasking is called process based multitasking
Ex: while type program
in editor we can here song in same system
Thread based
multitasking:
Executing several tasks
simultaneously where each task is a separate independent part is called a “Thread”
How to define a Thread:
1.By
extending Thread class
2.By
implementing Runnable Interface
Ex:
class Hello extends
Thread{
public void run(){
for (int i =
0;i<10;i++ ){
System.out.println("my
program by extendind Thread class.....");
}}}
class ThreadDemo{
public static
void main(String[] args){
Hello t=new Hello();
t.start();
for(int i=0;i<5;i++){
System.out.println("main thread");}}}
Difference between
t.start() and t.run() methods:
In case of t.start() a
new Thread will be created which is responsible for the execution of run()
method. But in the case of t.run() no new Thread will be created and run() method
will be executed just like normal method
by the main Thread.
If multiple Threads are
waiting to execute then which Thread will execute 1st is decided by
Thread Scheduler.
Activities of start():
1. Register Thread with
Thread Scheduler
2. Invoke run() method
We can say that without executing Thread class
start() method there is no chance of starting a new Thread in java
0 comments:
Post a Comment