What is Thread? How to Create a Thread?

In this content, you will learn what is thread and how to create a thread. A thread is sequential execution of a single set of instruction at the single point of time. A thread itself is not a program, it can not run on its own. Rather a thread run within a program.


creating thread
Thread



Creating Thread:
There are two bases of creating a thread.

  1. By creating a thread class that is creating a subclass of thread. In this way, we define a class that extended the thread class and overwrite the run method to provide an entry point to the thread execution.

The general form of the run method is 

public void run()
{
----------------
----------------
----------------
}



2. Creating a thread object finally, calling the start method to initiate the thread execution.

class NewThread extends Thread
{
New Thread()
{
super("Child Thread");
System.out.println(this);      //this its always present in current object//
}
public void run()
{
for(int i=1; i<=5; ++i);
System.out.println("\t from thread") ;


Post a Comment

0 Comments