The Java run-time system depends on threads for many things. Threads reduce inefficiency by preventing the waste of CPU cycles.
Threads exist in several states:
- New - When we create an instance of Thread class, a thread is in a new state.
- Running - The Java thread is in running state.
- Suspended - A running thread can be suspended, which temporarily suspends its A suspended thread can then be resumed, allowing it to pick up where it left off.
- Blocked - A Java thread can be blocked when waiting for a resource.
- Terminated - A thread can be terminated, which halts its execution immediately at any given Once a thread is terminated, it cannot be resumed.
Threads can be created by using two mechanisms:
- Extending the Thread class
- Implementing the Runnable Interface
Thread creation by extending the Thread class
We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.
public class MyClass extends Thread {
public void run(){
System.out.println("MyClass running");
}
}
To create and start the above thread:
MyClass t1 = new MyClass (); t1.start();
//or
//new MyClass().start();
Example Program:
class ThreadA extends Thread{
public void run() {
for(int i=1;i<=5;i++) {
System.out.println("Running thread "+i+" From Class A");
}
System.out.println("Exit from Class A");
}
}
class ThreadB extends Thread{
public void run() {
for(int j=1;j<=5;j++) {
System.out.println("Running thread "+j+" From Class B");
}
System.out.println("Exit from Class B");
}
}
class ThreadC extends Thread{
public void run() {
for(int k=1;k<=5;k++) {
System.out.println("Running thread "+k+" From Class C");
}
System.out.println("Exit from Class C");
}
}
public class ThreadExample {
public static void main(String[] args) {
new ThreadA().start(); new ThreadB().start(); new ThreadC().start();
}
}
Output
Running thread 1 From Class A
Running thread 2 From Class A
Running thread 3 From Class A
Running thread 4 From Class A
Running thread 5 From Class A Exit from Class A
Running thread 1 From Class B
Running thread 2 From Class B
Running thread 3 From Class B
Running thread 4 From Class B
Running thread 5 From Class B
Exit from Class B
Running thread 1 From Class C
Running thread 2 From Class C
Running thread 3 From Class C
Running thread 4 From Class C
Running thread 5 From Class C
Exit from Class C
We have simply initiated three new threads and started them. They are running concurrently on their own. Note that the output from the threads are not specially sequential. They do not follow any specific order. They are running independently of one another and each executes whenever it has a chance. Remember, once the threads are started, we cannot decide with certainty the order in which they may execute statements.
Thread creation by implementing the Runnable Interface
The easiest way to create a thread is to create a class that implements the Runnable interface.
To implement a Runnable interface, a class need only implement a single method called run(), which is declared like this:
public class MyClass implements Runnable {
public void run(){
System.out.println("MyClass running");
}
}
To execute the run() method by a thread, pass an instance of MyClass to a Thread in its constructor. Here is how that is done:
//MyClass obj=new MyClass();
//Thread t1=new Thread(obj);
//t1.start();
//or
Thread t1 = new Thread(new MyClass ()); t1.start();
Example Program
class ThreadA implements Runnable{
public void run() {
for(int i=1;i<=5;i++) {
System.out.println("Running thread "+i+" From Class A");
}
System.out.println("Exit from Class A");
}
}
class ThreadB implements Runnable{
public void run() {
for(int j=1;j<=5;j++) {
System.out.println("Running thread "+j+" From Class B");
}
System.out.println("Exit from Class B");
}
}
class ThreadC implements Runnable{
public void run() {
for(int k=1;k<=5;k++) {
System.out.println("Running thread "+k+" From Class C");
}
System.out.println("Exit from Class C");
}
}
public class ThreadExample {
public static void main(String[] args) {
Thread t1=new Thread(new ThreadA());
Thread t2=new Thread(new ThreadB());
Thread t3=new Thread(new ThreadC());
t1.start();
t2.start();
t3.start();
}
}
Output
Note: Output can be different on each run because thread doesn’t follow sequential order.