start thread java code example

Example 1: creating thread in java example

class Multi3 implements Runnable{  
public void run(){  
System.out.println("thread is running...");  
}  
  
public static void main(String args[]){  
Multi3 m1=new Multi3();  
Thread t1 =new Thread(m1);  
t1.start();  
 }  
}

Example 2: start thread java

package com.tutorialspoint;

import java.lang.*;

public class ThreadDemo implements Runnable {

   Thread t;
   ThreadDemo() {
    
      // thread created
      t = new Thread(this, "Admin Thread");
     
      // prints thread created
      System.out.println("thread  = " + t);
      
      // this will call run() function
      System.out.println("Calling run() function... ");
      t.start();
   }

   public void run() {
      System.out.println("Inside run()function");
   }

   public static void main(String args[]) {
      new ThreadDemo();
   }
}

Example 3: create thread java

class RunnableObject implements Runnable {
   private Thread t;
   private String threadName;
   
   RunnableObject( String name) {
      System.out.println("Creating " +  threadName );
      threadName = name;
      t = new Thread (this, threadName);
   }
   
   public void run() {
      System.out.println("Running " +  threadName );
   }
   
   public void start () {
      System.out.println("Starting " +  threadName );
      t.start ();
   }
}

public class TestThread {

   public static void main(String args[]) {
      RunnableObject R1 = new RunnableObject( "Thread-1");
      R1.start();
   }   
}

Tags:

Java Example