Menu
Topics Index
...
`

Creating Normal Thread - Java Example Program


Creating Normal Thread
class MyThread extends Thread
{
    public static void main(String[] args)
    {
    
        MyThread thread = new MyThread();
        System.out.println("Created thread by extending Thread");
        thread.start();
    
    }
    
    public void run()
    {
        System.out.println("Thread running...");
    }
}
OUTPUT

Created thread by extending Thread
Thread running...

DESCRIPTION

In the above program MyThread extends Thread so when we create objects for MyThread they behave as threads. When we invoke thread's start method it automatically generates a call to thread's run method.

THINGS TO TRY
  • Create a thread and invoke start method use below code if necessary
    MyThread newThread = new MyThread();
    newThread.start();
  • Place the below code inside the run method and execute the program.
    for(int i = 1; i <= 4; i++)
    {
        System.out.print(i +"\t");
    }

© meritcampus 2019

All Rights Reserved.

Open In App