What is MultiThreading in Java?
Multithreading in Java is a process of executing multiple threads simultaneously. A thread is the smallest unit of the processing. Multithreading and Multiprocessing, both are used to achieve multitasking.
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.
Disadvantages of MultiThreading
- Multiple threads can interfere with each other when sharing hardware resources such as caches or transaction lookaside buffers(TLBs).
- The execution time of a single thread can be degraded, even when only one thread is executing.
- Complex debugging and testing process.
What is Thread in Java?
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.
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)
- {
-
- Thread t = Thread.currentThread();
-
-
- System.out.println("Current Thread is " + t);
-
-
- t.setName("MyThread");
-
-
- t.setPriority(4);
-
-
- System.out.println("Current Thread is " + t);
-
- try {
- for (int i = 5; i > 0; i--) {
- System.out.println("*");
-
-
- Thread.sleep(1000);
- }
-
-
- } catch (InterruptedException e) {
- System.out.println(e);
- }
- }
- }
Output
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() {
-
- t = new Thread(this);
-
-
- t.start();
- }
-
- 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 JavaThreadRunnable {
- 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
Summary
In this article, we learned about multiThreading in Java and creating the threads in Java progrms.
To read more about MultiThreadin in Java
Click