In previous chapter, we have seen how to use Thread (java.lang.Thread) class to create thread in Java language. In this chapter we will see how to use Runnable (java.lang.Runnable) Interface to create thread in Java language.
1. Runnable Interface
- Runnable interface is present in java.lang package.
- Only one method is declared inside Runnable interface.
- Method declaration: public void run();
- As Runnable is interface, we need help of Thread.start() method to start the thread, while creating Thread using Runnable interface. We will see how to do it.
2. Using Runnable Interface to create Thread.
Check program compilation and execution here! jDoodle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class MyRunnable implements Runnable { public void run() { for (int i = 0; i < 10; i++) { System.out.println("MyRunnable Thread."); } } } public class MyRunnableDemo { public static void main(String[] args) { MyRunnable r = new MyRunnable(); Thread t = new Thread(r); // pass the Runnable object while new creating Thread object. t.start(); for (int i = 0; i < 10; i++) { System.out.println("Main Thread."); } } } |
3. Two ways of Thread creation in Java.
4. Different combinations of t.start() and t.run() methods.
Consider below program and let’s see the what each line does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class MyRunnable implements Runnable { public void run() { System.out.print("Running Thread => "); System.out.print(Thread.currentThread().getName()); System.out.println("\n--------------"); } } public class MyRunnableDemo { public static void main(String[] args) { /* Creating new MyRunnable object */ MyRunnable r = new MyRunnable(); /* Creating new Thread object without passing MyRunnable object. */ Thread t1 = new Thread(r); /* Creating new Thread object without passing MyRunnable object (Target Runnable). */ Thread t2 = new Thread(); t1.start(); // Case 1 (Creates new Thread, and execute our MyRunnable run() method) t2.start(); // Case 2 (Creates new Thread, but execute blank run() method from Thread class) t1.run(); // Case 3 (main thread executes our MyRunnable run() method, just like normal method call) t2.run(); // Case 4 (main thread executes Thread class run() method, just like normal method call) r.start(); // Case 5 r.run(); // Case 6 (main thread executes our MyRunnable run() method, just like normal method call) } } |
Case I (line 19): t1.start()
New Thread will be created, which is responsible for execution of run() method present in our MyRunnable class. This is usually the correct way to create Thread using Runnable interface.
Case II (line 20): t2.start()
New Thread will be created, which is responsible for execution of run() method present our Thread class. While creating t2 object we didn’t pass the target Runnable object, hence it called run() method from Thread class instead of run() method from our class.
Case III (line 22): t1.run()
New thread will not be created, but run() method will be called from MyRunnable class. (Only Thread.start() method is responsible for creating new Thread as we have seen in previous chapter.)
Case IV (line 23): t2.run()
New thread will not be created, but run() method will be called from Thread class. run() method in Thread class has blank implementation, so nothing will print when it executes. (Only Thread.start() method is responsible for creating new Thread as we have seen in previous chapter.)
Case V (line 25): r.start()
It will give Compile time error.
error: cannot find symbol | symbol: method start() | location: variable r of type MyRunnable | 1 error
Case V (line 26): r.run()
New Thread will not be created. But run() method present in Runnable class will be executed just like normal method call.
In second point, we have covered how to create Thread using Runnable interface, now let’s see which is better way to create the Thread and why? See you in next chapter.