A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

What is a Thread? How many ways can you define Thread?

A independent task in Thread-based Multitasking is called a Thread.

We can define a Thread in the following two ways:

(a) By extending Thread class
(b) By implementing Runnable interface.

(a) By extending Thread class

class myThread  extends Thread
{
  public void run()
  {
  for(int i=1; i<=10; i++)
  {
  System.out.println("Child thread is running "); 
   }
  }
 }
class ThreadExtend
{
  public static void main(String args[]) throws InterruptedException
  {
  myThread t = new myThread();
  t.start();
 for(int i=1; i<=10;i++)
  {
  System.out.println("Main thread ");
   }
  }
}
Output:

LEARN TUTORIALS

.

.