A thread in Java is the path followed when executing a program. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM) at the program’s start, when the main() method is invoked.
In Java, creating a thread is accomplished by implementing an interface and extending a class. Every thread in Java is created and controlled by the java.lang.Thread class.
A single-threaded application has only one Java thread and can handle only one task at a time. To handle multiple tasks in parallel, multi-threading is used: multiple Java threads are created, each performing a different task.
A question that often arises is how does a Java thread differ from a Java process.
A Java process is a program in execution. A Java thread is a subset of a Java process.
A Java process consists of multiple threads and a Java thread is often regarded as a light-weight process.
While a Java process has its own address space, a Java thread uses the process’ address space and shares it with other threads of that process. A thread can communicate with other threads of the same process using methods like wait(), notify(), notifyAll(). Global variables can also be used to pass data between threads. On the other hand, a process can only communicate with another process by using inter-process communication techniques such as sockets, files, etc.
Most commercial Java applications use multi-threading extensively. This is done for several reasons, explained in the multi-thread examples below:
A longer article explaining Java thread is available, here: https://www.eginnovations.com/blog/java-threads/. This article also covers topics such as: