Introduction to Threading and Concurrency in Java

What is Thread in Java?

Before discussing threads in Java, we first understand that every Java program is at the minimum a single threaded application. When a single threaded Java program executes, statements inside main() are executed sequentially one after the other; that single sequential flow of control is called a thread or thread of control. So, in the simplest terms, every running Java program has at least one thread and a thread is a single sequential flow of control within a program. In a single-threaded runtime environment actions execute one after the other. If we have more than one action to execute in parallel then we need multiple threads (equal to the number of executing actions).

How Single Threaded Java Program Executes?

As we said every Java program has at least one thread of control. When a Java program is executed; the execution starts from main() method and statements inside main() are executed sequentially one after the other. In a single threaded environment, at a time only one statement is executed and no parallel execution takes place. When a Java program starts, the JVM creates a thread -- the main thread and calls the program's main() method within that thread to execute statements contained inside body of main() method.

Multi-threading comes into picture when we have requirement to execute two or more methods in parallel. In case we need more than one function to be executed in parallel, we have to create threads at our own to run them concurrently. When two or more functions run in parallel, they are called concurrent modules and this feature of programming language is called concurrency. Java language has built-in support for concurrency and provides APIs for creating, using and managing threads to harness multi-threading feature of the language.

Multi-threading in Java

Languages such as C and C++ do not have built-in multi-threading capabilities and must therefore make non portable calls to operating system multi-threading primitives. Java, on the other hand, includes multi-threading primitives as part of the language itself and as part of its libraries. This facilitates manipulating threads in a portable manner across platforms.

Advantages of Multi-threading

Multi-threaded applications run actions in parallel; therefore, small tasks have not to wait for lengthy tasks to finish. They can run in parallel to big and time consuming tasks. In single-threaded environment one task must complete before other can begin.

In a multi-threaded application, threads can be distributed across multiple processors (if they are available) so that multiple tasks are performed concurrently and the application can operate more efficiently.

Multi-threading can also increase performance on single-processor systems that simulate concurrency—when one thread cannot proceed, another can use the processor.

Thread vs Process

Threads are sometimes referred to as lightweight processes. Like processes, threads are independent, concurrent paths of execution through a program, and each thread has its own stack, its own program counter, and its own local variables. However, threads within a process are less insulated from each other than separate processes are. They share memory, file handles, and other per-process state. Following are some differences between threads and processes.

Table 1: Difference between process and thread
Process Thread
A process has separate virtual address space. Two processes running on the same system at the same time do not overlap each other. Threads are entities within a process. All threads of a process share its virtual address space and system resources but they have their own stack created.
Every process has its own data segment All threads created by the process share the same data segment.
Processes use inter-process communication techniques to interact with other processes. Threads do not need inter process communication techniques because they are not altogether separate address spaces. They share the same address space; therefore, they can directly communicate with other threads of the process.
Process has no synchronization overhead in the same way a thread has. Threads of a process share the same address space; therefore synchronizing the access to the shared data within the process's address space becomes very important.
Child process creation within from a parent process requires duplication of the resources of parent process Threads can be created quite easily and no duplication of resources is required.

Last Word

In this tutorial we presented a brief overview of multi threading and concurrency model of Java, advantages of multi threading and difference between a process and a thread. Hope you have enjoyed reading this tutorial, please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.