Introduction
In this article, we discuss how to create a thread in Java and also discuss Multi-threading and Multi-tasking in Java.
Multi-threading
Multi-threading is a process of running/executing multiple threads simultaneously (at the same time).
A thread is basically a lightweight sub-process (the smallest unit of processing). The term multi-threading and multi-processing are both used to do multitasking. But we use multi-threading rather than multi-processing because the behavior of threads are to share a common memory area. They don't allocate separate memory so save memory, and context-switching between the threads takes less time than processes. Multi-threading is mostly used in games, animation, and so on.
Multi-tasking
Multi-tasking is a process of executing multiple tasks at a time. We use multi-tasking to utilize the CPU. It can be done by one of two ways;
- Thread based multitasking
- Process-based multitasking
1. Thread based
The features of Thread based are:
- Threads share the same address space.
- Threads are lightweight.
- The cost of communication between threads within a process is low.
2. Process-based
The features of Process-based are:
- The cost of communication between processes is high.
- Moving from one process to another requires some time for saving and loading registers, memory, maps, updating lists, etcetera.
- Each process has its own memory address, in other words, each process allocates a separate memory area.
- It is heavyweight in nature.
How to create a thread in Java?
There are two ways to create a thread; they are:
- By extending the Thread class.
- By implementing the Runnable interface.
Thread class
This class provides constructors to create and perform operations on a thread. The Thread class extends the Object class and implements the Runnable interface.
Thread class Constructors
Some commonly used constructors of the thread class are:
- Thread()
- Thread(String sname)
- Thread(Runnable run)
- Thread(Runnable run, String sname)
Thread class Public methods
Some commonly used public methods of the Thread class are:
void yield()
used to cause the currently executing thread object to temporarily pause and allow other threads to execute.
used to cause the currently executing thread object to temporarily pause and allow other threads to execute.
void suspend()
used to suspend the thread.
used to suspend the thread.
void resume()
used to resume the suspended thread.
used to resume the suspended thread.
void stop()
used to stop the thread.
used to stop the thread.
void run()
used to start an action for a thread.
used to start an action for a thread.
void start()
starts the execution of the thread.
starts the execution of the thread.
void sleep()
puts the currently executing thread to sleep.
puts the currently executing thread to sleep.
void join()
waits for a thread to die.
waits for a thread to die.
int getPrioprity()
used to return the priority of a thread
used to return the priority of a thread



Join the conversation! Your thoughts help the community grow.