Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
hackerss
hackerss

Posted on

Create a class that extends from thread and have an example in Java

Create a class that extends from thread and have an example - Java:


public class ThreadExample extends Thread {
   /**
   * This method is the constructor of the class.
   * @param name name of the thread.
   */   
   public ThreadExample(String name) {
       super(name);
   }

   /**
   * This method is the run method of the class.
   */   
   public void run() {
       //Print the name of the thread
       System.out.println("Thread name is :" + getName());
   }

   /**
   * One Example
   */
   public static void main(String args[]) throws IOException {

      ThreadExample  threadExample= new ThreadExample ("Thread 1");
      //Start the thread
      threadExample.start();
      //Example 2
      ThreadExample  threadExample2= new ThreadExample ("Thread 2");
      //Start the thread
      threadExample2.start();

   }
}
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)