What is Thread, How to Use Threads?



Def. of Thread: A Thread represents a process (or)
execution of a statment one by one by the Microprocessor(JVM) is called Thread.

Note: Every java program will have a thread.

The basic usage of the thread is as follows:

In the most general sense, you can create a thread by instantiating an object of type thread.
There are two ways to create a new thread.

–> You can Extend the Thread class in your class and override the method run().

Syntax :

public class MyThread extends Thread
{
public void run()
{
——
}
}

–> You can implement the Runnable interface and use that class in the Thread constructor

Syntax :

public class MyThread implements Runnale
{
public void run()
{
——
}
}

Share with SociBook.com

Random Posts

Post a Comment