Java Inline Thread Creation

Java Inline Thread Creation | In Java, a thread that is created with an anonymous inner class or by using lambda expression is called an inline thread.

Anonymous thread object means a thread that is created without extending from Thread class or implementing from Thread class or implementing from the Runnable interface explicitly is called “anonymous” thread object. The other name for the anonymous thread object is the “inline” thread.

Using an anonymous inner class we can create Thread without extending from Thread class or implementing from Runnable interface explicitly.

Inline Thread Creation Using Thread class

Syntax to create an inline thread using Thread class,

new Thread() {
  public void run(){
    //code.....
  }
}.start();

Creating inline thread using Thread class

 // Inline Thread using Thread class
 class CustomThread{
   public static void main(String[] args) {
     System.out.println("main thread execution started");
     
     new Thread() {
       public void run(){
         for(int i=1; i<=5; i++){
           System.out.println("run: "+i);
         }
       }
     }.start();
     
     System.out.println("main thread execution completed");
   }
 }

Output:-

main thread execution started
main thread execution completed
run: 1
run: 2
run: 3
run: 4
run: 5

One custom thread is created and the run() method is exected from the anonymous inner class. Along with the main thread, the inline thread is executed concurrently.

Inline Thread Creation using Runnable Interface

Syntax to create an inline thread using Runnable interface,

new Thread(
  new Runnable(){
    public void run(){
      //code....
    }
  }
).start();

Inline thread creation using Runnable interface

class InlineThread_Runnable{
   public static void main(String[] args) {
     System.out.println("main thread execution started");
     System.out.println("Inline thread creation using Runnable interface");
     
     new Thread(
       new Runnable(){
         public void run(){
           System.out.println("Inline thread execution started");
           for(int i=0; i<=1; i++){
             System.out.println("run :"+i);
           }
           try{Thread.currentThread().sleep(200);}
           catch(InterruptedException e){}
           System.out.println("Inline thread execution completed");
         }
       }
     ).start();
     
     try{Thread.currentThread().sleep(100);}
     catch(InterruptedException e){}
     System.out.println("main thread execution completed");
   }
} 

Output:-

main thread execution started
Inline thread creation using Runnable interface
Inline thread execution started
run: 0
run: 1
main thread execution completed
Inline thread execution completed

Inline Thread Creation using Lambda Expression

Lambda expression syntax for inline thread from the Runnable interface,

new Thread( ()-> /* code..*/).start();

Inline thread creation using Lambda expression

class InlineThread_Runnable {
   public static void main(String[] args) {
     System.out.println("main thread execution started");

     new Thread(
       ()-> System.out.println("Inline thread")
       //Only one statement so semicolon isn't required.
     ).start();

     System.out.println("main thread execution completed");
   }
} 

Output:-

main thread execution started
main thread execution completed
Inline thread

When we should use an inline thread?
If we want to thread representation (some implementation) only one time not repeatedly then we should use inline thread. But if we want to use implementation repeatedly with the concurrent flow of execution then instead of using inline thread, we should create thread objects by using Thread class or through Runnable interface.

Note:- Inline thread is not the “official” Java term. It is invented by textbook authors or software industry experts.

Useful links:-

Also learn:-

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *