Java program on MultiThreading by extending Thread class Example 3
THREAD:
A thread is a light-weight smallest part of a process that can run concurrently with the other threads of the same process. All threads of a process share the common memory.MULTITHREADING:
The process of executing multiple threads simultaneously is known as multithreading.
PROGRAM:
class ThreadA extends Thread {
ThreadA() {
System.out.println("Hey I'm Constructor (o_o)");
start();
}
public void run() {
System.out.println("Hello I am a Thread A (^-^)");
}
}
class ThreadB extends Thread {
ThreadB() {
System.out.println("Hey I'm Constructor (*_*)");
start();
}
public void run() {
System.out.println("Hello I am a Thread B ('-')");
}
}
class ThreadingDemo {
public static void main(String[] args) {
ThreadA t1 = new ThreadA();
ThreadB t2 = new ThreadB();
System.out.println("Hi I am main (-_-)");
}
}
ThreadA() {
System.out.println("Hey I'm Constructor (o_o)");
start();
}
public void run() {
System.out.println("Hello I am a Thread A (^-^)");
}
}
class ThreadB extends Thread {
ThreadB() {
System.out.println("Hey I'm Constructor (*_*)");
start();
}
public void run() {
System.out.println("Hello I am a Thread B ('-')");
}
}
class ThreadingDemo {
public static void main(String[] args) {
ThreadA t1 = new ThreadA();
ThreadB t2 = new ThreadB();
System.out.println("Hi I am main (-_-)");
}
}
OUTPUT:
Hey I'm Constructor (o_o)Hello I am a Thread A (^-^)
Hey I'm Constructor (*_*)
Hi I am main (-_-)
Hello I am a Thread B ('-')