A thread is a small unit of the program.
There are two type
1) single-tasking.
2) Multitasking.
Single-tasking: Only one task is given to the Processor.
Disadvantage: In single tasking, we are wasting a lot of processor time & microprocessor has sat idle without any job for a long time.
class Test extends Thread {
public void run() {
for(int i=0;i<=5;i++) {
System.out.println(i);
}
}
}
class Test1 {
public static void main(String a[]) {
Test obj=new Test();
Thread t=new Thread(obj);
t.start();
}
class Test extends Thread {
public void run() {
void task1();
void task2();
void task3();
}
void task1() {
System.out.println(“Task1”);
}
void task2() {
System.out.println(“Task2”);
}
void task3() {
System.out.println(“Task3”);
}
}
class Test1 {
public static void main(String a[]) {
Test obj=new Test();
Thread t=new Thread(obj); t.start();
}
}
Multi tasking: More then one task simultaneously performs.
Multitasking is better than single-tasking: Multitasking is to use the processor time and it is not sitting idle. In this way, we can complete several tasking at a time.
class Test1 extends Thread {
public void run() {
for(int i=0;i<=5;i++) {
System.out.println(“Test1″+i);
}
}
}
class Test2 extends Thread {
public void run() {
for(int i=0;i<=5;i++) {
System.out.println(“Test2″+i);
}
}
}
class Test {
public static void main(String a[]) {
Test1 obj1=new Test1();
Test2 obj2=new Test2();
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
t1.start();
t2.start();
}
}