A thread is a lightweight process (part of a program) or flow of control within a Java program. The JVM allows a program/application to have more than one thread and the threads can run concurrently; such a program is called a Multithreaded Program and the approach is known as Multithreading.
Advantages of Multithreading
- Optimize performance of an application.
- Reduce the computation time.
- Saves memory, because threads share the same memory space.
- Context switching between threads is less expensive.
- Provides a specialized form of multitasking.
In Java's multithreading, the Thread Class & Runnable Interface are used to create and manage threads. Each and every Java program has a thread, i.e. the main thread. The main thread runs immediately when a Java program is started. All other threads are engendered from the main thread. The main thread must be the last thread to finish execution because it performs various shutdown actions, otherwise the system may hang.
The main thread is controlled using the currentThread() method; currentThread() is a public static member of the Thread class. The currentThread() method returns the reference to the thread in which it is called.
For example,
public class JavaThreadingDemo {
public static void main(String[] args) {
//Reference to the current thread is obtained and stored in t
Thread t = Thread.currentThread();
//Displays the information of thread
System.out.println("Current Thread is " + t);
//To change the name of the thread
t.setName("MyThread");
//To change the priority of the thread
t.setPriority(4);
//Again, displaying the information of thread
System.out.println("Current Thread is " + t);
try {
for (int i = 5; i > 0; i--) {
System.out.println("*");
//Pause for one second
Thread.sleep(1000);
}
//Sleep() method may throw InterruptedException, so we are wrapping the
//code in try-catch block
} catch (InterruptedException e) {
System.out.println(e);
}
}
Output
Current Thread is Thread[main,5,main]
Current Thread is Thread[MyThread,4,main]
*
*
*
*
*
Notice the first line of the output; it shows the information about the thread i.e., Thread[main,5,main], where main is the name of thread, 5 is the priority of the thread (default value) and main is thread group.
Create New Thread by Implementing Runnable Interface
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new thread, second thread
t = new Thread(this);
//"this" indicates that you need a new thread to call run() on this object
t.start(); // Start the thread, call run()
}
public void run() {
System.out.println("Inserting Child Thread.....");
try {
for (int i = 10; i > 0; i--) {
System.out.println("*");
Thread.sleep(500);
}
} catch (InterruptedException IEx) {
System.out.println(IEx);
}
System.out.println("Exiting Child Thread.....");
}
}
public class JavaApplication15 {
public static void main(String[] args) {
System.out.println("Inserting Main Thread....");
new NewThread();
try {
for (int i = 10; i > 0; i--) {
System.out.println("+");
Thread.sleep(1000);
}
} catch (InterruptedException IEx) {
System.out.println(IEx);
}
System.out.println("Existing Main Thread.....");
}
}
Output
Inserting Main Thread....
+
Inserting Child Thread.....
*
*
*
+
*
*
+
*
+
*
*
*
+
*
+
Exiting Child Thread.....
+
+
+
+
Existing Main Thread.....