MultiThreading in Java
What is the start() and run() method of Thread class?
1. Thread class basics
In Java, to create a thread you either:
-
Extend the
Thread
class and override therun()
method, OR -
Implement the
Runnable
interface and pass it to aThread
object.
2. run() method
-
Signature:
This method contains the actual logic/code that will be executed by the thread.
-
It does not start a new thread by itself.
-
If you call
run()
directly, it behaves like a normal method call (runs in the same main thread).
👉 Think of
run()
as the task of the thread.
3. start() method
-
Signature:
public synchronized void start()
Defined in the
Thread
class (not meant to be overridden).-
When you call
start()
, the JVM:-
Creates a new separate call stack for the new thread.
-
Internally calls the
run()
method of that thread in a new thread of execution.
-
👉 Think of start()
as the trigger that tells the JVM: "Please run this thread separately."
2. What is Thread in Java?
Threads are basically the lightweight and smallest unit of processing that can be managed independently by a scheduler. Threads are referred to as parts of a process that simply let a program execute efficiently with other parts or threads of the process at the same time. Using threads, one can perform complicated tasks in the easiest way. It is considered the simplest way to take advantage of multiple CPUs available in a machine. They share the common address space and are independent of each other.
3. What are the two ways of implementing thread in Java?
There are basically two ways of implementing thread in java as given below:
Extending the Thread class
class MultithreadingDemo extends Thread { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); obj.start(); } }
Output: My thread is in running state.
Implementing Runnable interface in Java
Example:
class MultithreadingDemo implements Runnable
{
public void run()
{
System.out.println("My thread is in running state.");
}
public static void main(String args[])
{
MultithreadingDemo obj=new MultithreadingDemo();
Threadtobj =new Thread(obj); tobj.start();
}
}
Output:
My thread is in running state.
4. What's the difference between thread and process?
Thread: It simply refers to the smallest units of the particular process. It has the ability to execute different parts (referred to as thread) of the program at the same time.
They share data and information with each other.
There is a need for synchronization in threads to avoid unexpected scenarios or problems.
Threads are parts of a process, so they are dependent on each other but each thread executes independently.
Process: It simply refers to a program that is in execution i.e., an active program. A process can be handled using PCB (Process Control Block).
They do not share data with each other.
There is no need for synchronization in each process.
Processes are independent of each other.
Comments
Post a Comment