[post-views]
In this lesson, you will learn.
A single-threaded application can perform only one task at a time. It waits for one task to be completed before another can start. As a result, the efficiency of the application is hampered.
Multiple or Multithreading model helps to perform multiple tasks simultaneously, which saves time and improves the execution speed and efficiency of the application.
It involves multiple threads and each thread shares the same memory space. Each thread performs the job independently of another thread.
When a Java program starts up, the one thread begins running immediately known as your program’s main thread.
The method currentThread( )
, which is a public static
member of a Thread
class returns a reference to the thread in which it is called.
Syntax
static Thread currentThread( )
package mainthread;
public class MainThreadExample {
public static void main(String[] args) {
// Get the current thread which is the main thread
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
System.out.println("Current thread: " + t.getName());
// Changing the name of the main thread
t.setName("PrimaryThread");
System.out.println("After name change: " + t.getName());
// Output details about the main thread
System.out.println("Main thread details: " + t);
// Simple loop to demonstrate the thread execution
for (int i = 0; i < 5; i++) {
System.out.println(i);
try {
Thread.sleep(400); // Pause the main thread for 400 milliseconds
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
}
}
}
Output
Current thread: Thread[main,5,main]
Current thread: main
After name change: PrimaryThread
Main thread details: Thread[PrimaryThread,5,main]
0
1
2
3
4
Thread[main,5,main],
This displays, in order: the name of the thread, its priority, and the name of its group.
By default, the name of the main thread is main. Its priority is 5, which is the default value, and main is the name of the group of threads to which this thread belongs.
The sleep( ) method pauses or suspends the thread execution by a specified period of milliseconds and throws InterruptedException,
Syntax: sleep() Methods
static void sleep(long milliseconds) throws InterruptedException
static void sleep(long milliseconds, int nanoseconds) throws InterruptedException
Here’s a table that outlines some of the key methods of the Thread
class along with a brief explanation and example for each.
Method | Description | Example |
---|---|---|
start() |
Starts the thread by calling the run() method of the thread. |
Thread t = new Thread(() -> System.out.println("Running")); t.start(); |
run() |
Contains the code that is executed by the thread. Overriding this method is how you define thread behavior. | public void run() { System.out.println("Thread is running."); } |
sleep(long millis) |
Causes the currently executing thread to sleep (temporarily cease execution) for the specified time. | try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } |
join() |
Waits for the thread to die, i.e., completes its execution. | Thread t = new Thread(() -> System.out.println("Running")); t.start(); t.join(); |
interrupt() |
Interrupts the thread, causing it to continue execution if it was blocked for or sleeping. | Thread t = new Thread(() -> { try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("Interrupted!"); } }); t.start(); t.interrupt(); |
yield() |
A static method causing the currently executing thread object to temporarily pause and allow other threads to execute. | Thread.yield(); |
currentThread() |
Returns a reference to the currently executing thread object. | Thread t = Thread.currentThread(); System.out.println("Current thread: " + t.getName()); |
isAlive() |
Tests if the thread is alive (has started and has not yet died). | Thread t = new Thread(() -> System.out.println("Running")); t.start(); System.out.println(t.isAlive()); |
setName(String name) |
Sets the name of the thread. | Thread t = new Thread(() -> System.out.println("Running")); t.setName("MyThread"); |
getName() |
Returns the name of the thread. | Thread t = new Thread(() -> System.out.println("Running")); t.start(); System.out.println(t.getName()); |
setPriority(int newPriority) |
Changes the priority of the thread. Thread priorities are set in a range between MIN_PRIORITY (1) and MAX_PRIORITY (10). |
Thread t = new Thread(() -> System.out.println("Running")); t.setPriority(Thread.MAX_PRIORITY); |
getPriority() |
Returns the priority of the thread. | Thread t = new Thread(() -> System.out.println("Running")); System.out.println(t.getPriority()); |
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.