In this blog we will know about threading in java.
Thread: - A single flow of control in a program is
known as thread. This can execute independently in a processing environment and
perform an operation. A program can be divided into different part or different
sequential flow of control known as multithreaded program.
How to create a thread
A thread can be created by a java program by using
Creating of thread using thread class
1.create a class by inheriting thread class if required
provide constructor to the class.
2.Override run () method to provide the logic for thread,
which can execute independently, run () method contains thread body.
3.Create an interface if the class which is created in step-1
4.To start the threads execution use start () method which
calls the run () method.
class mythread extends Thread
public void run()
System.out.println("Welcome to multithreading "+i );
public class threadDemo
void main(String arg)
Creating thread using runnable interface
1.Create a class, which must implement java.lang.Runnable
2.Override run () method to provide body of the thread.
3.Create an instance of thread class which must accepts an
instance of the class created in step-1 (sub class of runnable interface).
4.Start the execution of the thread call start method in the
thread class instance.
System.out.println("Provide ur number");
System.out.println("No is "+x);
public class thread_demo
void main(String arg)
//to call run() of
mythread class use its object in constructor of Thread class
/* join() suspends
parent thread's execution till the child upon which its invoked is not
isAlive() returns true if thread is not finished else false
Difference between multiprocessing and multithreading
1.A program in execution is known as process. Multiple
processes for single environment are known as multiprocessing.
2.Minimum unit of multiprocessing is a program.
3.Process occupies its own resources or memory.
1.A part of a program, which can execute independently, is
known as Multithreading.
2.Minimum unit of Multithreading is thread or part of a
3.Thread occupies the resources available to its process.
This also being called as lightweight process.