Multithreading is a programming technique that allows multiple threads of execution to run concurrently within the same process. In other words, it enables a single program to perform multiple tasks at the same time. This can lead to significant performance improvements, as it can allow the program to take advantage of the multiple cores available in modern CPUs. We can say that it refers to a process of executing two or more threads simultaneously for maximum utilization of the CPU.
What exactly is thread ?
In Java, a thread is a lightweight unit of execution that runs within a larger program. Threads allow multiple tasks to be executed concurrently within a single program. In other words, a thread is a separate flow of control that can run simultaneously with other threads. Each thread in Java has its own call stack and program counter, allowing it to execute independently of other threads. Threads can be created using the Thread class or the Runnable interface, which provides a way to implement thread functionality in classes that do not inherit from Thread.
To create a new thread using the Thread class, you can either extend the Thread class or create an instance of the Thread class and pass a Runnable object to its constructor. Here is an example of how to create a thread using the Thread class:
class MyThread extends Thread { public void run() { System.out.println("Thread is running"); } } public class Main { public static void main(String[] args) { MyThread t = new MyThread(); t.start(); } } In this example, we define a new class called MyThread that extends the Thread class. We override the run() method, which is the entry point for the thread. In the main method, we create an instance of MyThread and call its start() method to start the thread.
When the start() method is called, the JVM creates a new thread and calls the run() method on that thread. The run() method contains the code that the thread will execute. In this example, the run() method simply prints a message to the console.
Threads in Java can be used to improve the performance and responsiveness of a program by allowing multiple tasks to be executed concurrently. However, care must be taken when working with threads to avoid race conditions and other synchronization issues that can lead to unpredictable behavior.
What is thread lifecycle ?

1. New
The thread is the new state when we create it using the “Thread class”. It remains in this state until the program starts the thread by calling the start() method. It is also called a born thread.
2. Runnable
In this phase, the start() method invokes the instance of the thread. The scheduler takes the thread control to finish the execution. It depends on the scheduler whether to run the thread or not.
3. Running
The thread goes to the running state when its execution starts. The scheduler selects one thread from the thread pool and the thread starts executing in the application.
4. Waiting
There is a need for synchronization between threads as multiple threads are running in the application. Hence, one thread has to wait, till the other thread finishes its execution. Therefore, we say that the thread is in the waiting state.
5. Dead
When the thread is terminated, the thread goes into the dead state.
Thread priorities
Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled. Java thread priorities are in the range between MIN_PRIORITY a constant of1 and MAX_PRIORITY a constant of 10. By default, every thread is given priority NORM_PRIORITY a constant of 5.
Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and very much platform dependent.
Thread Methods:
1. public void start
Starts the thread in a separate path of execution, then invokes the run method on this
Thread object.
2 public void run
If this Thread object was instantiated using a separate Runnable target, the run method is
invoked on that Runnable
object.
3 public final void setNameStringname
Changes the name of the Thread object. There is also a getName method for retrieving the
name.
4 public final void setPriorityintpriority
Sets the priority of this Thread object. The possible values are between 1 and 10.
5 public final void setDaemonbooleanon
A parameter of true denotes this Thread as a daemon thread.
6 public final void join(longmillisec)
The current thread invokes this method on a second thread, causing the current thread to
block until the second thread terminates or the specified number of milliseconds passes.
7 public void interrupt
Interrupts this thread, causing it to continue execution if it was blocked for any reason.
Conclusion
Multithreading is a powerful technique that can help you write more efficient and responsive programs. However, it also introduces a number of complexities that you'll need to aware of.
Kommentare