In this post, we will see how to create a thread in java. There are two ways to create a thread in java.
- Extending Thread class
- Implementing Runnable interface
Example of thread creation extending Thread class –
package multithreading; import java.util.Set; import java.util.concurrent.Callable; public class ThreadExample extends Thread { @Override public void run() { System.out.println("Thread created using Thread class"); System.out.println(Thread.currentThread().getName()); } public static void main(String[] args) { ThreadExample threadExample = new ThreadExample(); threadExample.start(); } }
Output is –
Thread created using Thread class
Thread-0
In the next example, we will see how to create a thread using the Runnable interface. But before going ahead let’s see what’s going on in the above example. Did you notice, we didn’t call run() method explicitly but we have output. How run() method is getting called? As soon as threadExample.start() will execute internally JVM will call run() method. We have a separate post which tells how start() method calls run() method internally.
Example of thread creation implementing Runnable interface –
package multithreading; import java.util.Set; import java.util.concurrent.Callable; public class ThreadExample implements Runnable{ @Override public void run() { System.out.println("Thread created using runnable interface"); System.out.println(Thread.currentThread().getName()); } public static void main(String[] args) { ThreadExample threadExample = new ThreadExample(); Thread thread = new Thread(threadExample); thread.start(); } }
Output is –
Thread created using runnable interface
Thread-1
This is all about basic, how to create a thread in java. We can also create a thread in a different way i.e as an anonymous inner class. Let’s see an example.
package multithreading; import java.util.Set; import java.util.concurrent.Callable; public class ThreadExample extends Thread { public static void main(String[] args) { Thread thread1 = new Thread() { public void run() { System.out.println("Using Thread class "+Thread.currentThread().getName()); } }; thread1.start(); Runnable r = new Runnable() { public void run() { System.out.println("Using Runnable interface "+Thread.currentThread().getName()); } }; Thread thread2 = new Thread(r); thread2.start(); } }
Output is –
Using Runnable interface Thread-1
Using Thread class Thread-0
[stextbox id=’info’]Note – In the above program output may vary because we have two different thread and thread scheduler decides(using some algorithm) which thread is going to execute first. [/stextbox]
In the last couple of examples, we saw run() method is automatically getting called by JVM when we call start() method. Can we call run() method explicitly? If yes will it behave the same as while it getting called through the start() method? We can call run() method explicitly but it would behave like a normal method and yes we will not have benefits of multithreading.
package multithreading; import java.util.Set; import java.util.concurrent.Callable; public class ThreadExample implements Runnable { @Override public void run() { System.out.println("this will behave as normal method"); } public static void main(String[] args) { ThreadExample threadExample = new ThreadExample(); threadExample.run(); } }
Output is – this will behave as normal method
In the next example, we will see what will happen if we try to call twice with the same thread? We can’t start a thread twice it will throw IllegalThreadStateException exception.
package multithreading; import java.util.Set; import java.util.concurrent.Callable; public class ThreadExample implements Runnable{ @Override public void run() { System.out.println("thread created using runnable interface"); } public static void main(String[] args) { ThreadExample threadExample = new ThreadExample(); Thread thread = new Thread(threadExample); thread.start(); thread.start(); } }
Output is –
Exception in thread “main” thread created using runnable interface
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at multithreading.ThreadExample.main(ThreadExample.java:17)
Summary : –
- We can create a thread extending Thread class or Runnable interface.
- The run() method will get called internally when we call start() method.
- We can’t start a thread twice, it will throw IllegalThreadStateException.
- We can call run() method explicitly but it will not create a separate call stack, treated as a normal method.