Multithreading in Java: Execute Multiple Threads Concurrently

Learn about multithreading in Java, a powerful feature enabling the concurrent execution of multiple threads. Discover how threads, as lightweight sub-processes, share memory space and help optimize memory usage and reduce context-switching time for efficient performance in Java applications.



Multithreading in Java

Multithreading in Java allows executing multiple threads simultaneously. A thread is a lightweight sub-process, sharing memory space. It saves memory and context-switching time.

Syntax

public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output

Hello World

Advantages of Multithreading

  1. Threads are independent; multiple operations can be performed simultaneously.
  2. It saves time as many operations can be done together.
  3. If an exception occurs in one thread, it doesn't affect others.

Multitasking

Multitasking allows executing multiple tasks simultaneously, utilizing the CPU.

Two types of multitasking:

  • Process-based Multitasking (Multiprocessing)
    • Each process has a separate memory area.
    • Processes are heavyweight.
    • High communication cost between processes.
  • Thread-based Multitasking (Multithreading)
    • Threads share the same memory area.
    • Threads are lightweight.
    • Low communication cost between threads.

Java Thread

A thread is a separate path of execution. Threads are independent and share memory.

Java Thread Class

The Thread class provides methods to create and manage threads.

Java Thread Methods

Modifier and Type Method Description
void start() Starts the execution of the thread.
void run() Performs an action for a thread.
static void sleep() Pauses the thread for a specified time.
static Thread currentThread() Returns a reference to the currently executing thread.
void join() Waits for the thread to die.
int getPriority() Returns the thread's priority.
void setPriority() Changes the thread's priority.
String getName() Returns the thread's name.
void setName() Changes the thread's name.
long getId() Returns the thread's ID.
boolean isAlive() Tests if the thread is alive.
static void yield() Pauses the current thread to allow other threads to execute.
void suspend() Suspends the thread.
void resume() Resumes the suspended thread.
void stop() Stops the thread.
void destroy() Destroys the thread group and all its subgroups.
boolean isDaemon() Tests if the thread is a daemon thread.
void setDaemon() Marks the thread as a daemon or user thread.
void interrupt() Interrupts the thread.
boolean isInterrupted() Tests if the thread has been interrupted.
static boolean interrupted() Tests if the current thread has been interrupted.
static int activeCount() Returns the number of active threads in the current thread group.
void checkAccess() Checks if the current thread has permission to modify the thread.
static boolean holdsLock(Object obj) Returns true if the current thread holds the monitor lock on the specified object.
static void dumpStack() Prints a stack trace of the current thread to the standard error stream.
StackTraceElement[] getStackTrace() Returns an array of stack trace elements representing the thread's stack dump.
static int enumerate(Thread[] tarray) Copies active threads into the specified array.
Thread.State getState() Returns the thread's state.
ThreadGroup getThreadGroup() Returns the thread's group.
String toString() Returns a string representation of the thread.
void notify() Notifies one thread waiting for a specific object.
void notifyAll() Notifies all threads waiting for a specific object.
void setContextClassLoader(ClassLoader cl) Sets the context ClassLoader for the thread.
ClassLoader getContextClassLoader() Returns the context ClassLoader for the thread.
static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler() Returns the default handler for uncaught exceptions.
static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) Sets the default handler for uncaught exceptions.