![]() |
![]() |
Introduction of Errors | |
Java Exceptions | |
Java Try Block | |
Java Catch Block | |
Java Finally Block | |
Java Throw Keyword | |
Java Throws Keyword | |
Java Builtin Exceptions | |
User Defined Exceptions |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
When program begins its execution, one thread is started immediately,which is called as main thread of program.
Main thread is required because
Main thread is controlled by thread object.
Controlling main thread
In general the thread can be created by instantiating an object of type Thread. java defines two ways in which this can be accomplished.
Runnable
InterfaceThread
classThe easier way to create a Thread by implementing a class to runnable interface.
The Runnalbe interface have run () method which is required for starting the execution of Thread.
Steps
Example
Program to print first 10 numbers using Runnable interface.
class MyThread implements Runnable { public void run() { System.out.println("Thread started"); for(int i=1;i<=10;i++) { System.out.println("i : "+i); } System.out.println("Thread exited"); } } public class Creator { public static void main(String[] args) { MyThread mthread = new MyThread(); Thread t = new Thread(mthread); t.start(); } }
Output
Explaination
Note : This type of approach is used when we have already extended some class to mythread class. That time we cannot extend more than one class to a single class. Hence java allows us to use interface for creating a thread.
Example
class A implements Runnable { public void run() { System.out.println("Thread A started"); for(int i=1;i<=5;i++) System.out.println("A : "+i); System.out.println("Thread A exited"); } } class B implements Runnable { public void run() { System.out.println("Thread B started"); for(int i=1;i<=5;i++) System.out.println("B : "+i); System.out.println("Thread B exited"); } } class C implements Runnable { public void run() { System.out.println("Thread C started"); for(int i=1;i<=5;i++) System.out.println("C : "+i); System.out.println("Thread C exited"); } } public class Test { public static void main(String[] args) { A a1 = new A(); B b1 = new B(); C c1 = new C(); new Thread(a1).start(); new Thread(b1).start(); new Thread(c1).start(); } }
Output
Explaination
Second way of creating a thread is to extend the Thread class. This approach used when we want to leave a separate class for creating a thread only.
Steps
Example
Program to print 1 to 5 Numbers by Extending Thread class
class A extends Thread { public void run() { System.out.println("Thread started"); for(int i=1;i<=5;i++) System.out.println("i : "+i); System.out.println("Thread exited"); } } public class Test { public static void main(String[] args) { A a1 = new A(); a1.start(); } }
Output
Explaination
Example
class A extends Thread { public void run() { System.out.println("Thread A started"); for(int i=1;i<=5;i++) System.out.println("A : "+i); System.out.println("Thread A exited"); } } class B extends Thread { public void run() { System.out.println("Thread B started"); for(int i=1;i<=5;i++) System.out.println("B : "+i); System.out.println("Thread B exited"); } } class C extends Thread { public void run() { System.out.println("Thread C started"); for(int i=1;i<=5;i++) System.out.println("C : "+i); System.out.println("Thread C exited"); } } public class Test { public static void main(String[] args) { A a1 = new A(); B b1 = new B(); C c1 = new C(); a1.start(); b1.start(); c1.start(); } }
Output
Explanation