Multithreading in Java is a very interesting topic. Here we are giving you the details of Concurrency in Java, Process, Threads in Java.
Concurrency in Java
Computer system can do more than one thing at a time. So, computer can continue to work on playing music. At same time it can be writing contents in a notepad. And it can be printing files into printer. And you might be using browser which is downloading the files from the internet.
Even we expect a single application to perform more than one operation at a time. For example, responding to mouse and keyboard. At the same time in parallel showing the notifications. Also checking for any updated available for current application.
Applications that can perform such multiple things in parallel at the same time is known as concurrent application. Running applications concurrently is known as concurrency in Java.
Java is designed to support concurrent programming. Java included a high level concurrency APIs. Here we will discuss about basic concurrency support and some high level APIs in the java.util.concurrent packages.
In concurrent programming there are two basic units of execution
When we are talking about concurrent programming then, we are mostly talking about making use of threads.
Normal computer has many processes and threads. A core of processor is shared among processes and threads. Operating system makes use of time slicing feature to share the time of processor’s core among processes and threads.
Process in Java
Process is the instance of a program execution. For example, if you open two windows of a internet browser then you have two processes. It contains the program code and its activity.
Each process has a complete private set of run-time resources, like its own memory space. Process provides an execution environment for our code.
Process is made up of multiple threads of execution that execute instructions concurrently.
When we look at a single application like Google chrome, that can actually be a set of processes. To support communication between processes, operating systems support Inter Process Communication (IPC). IPC can be done using sockets and pipes. Inter process communication can also be possible between processes present in different systems (computer machine).
The life cycle of a process can be described by a state diagram. States represent the execution status of the process.
Operating system maintains the management information of a process in a Process Control Block (PCB). Processes can be divided into multiple threads.
Process Control Block contains following information
- Management information of resources
- Administrative information
- Snapshot of an execution
Process share its PCB to the threads, except the information related to current execution. This selective information what process share with thread is known as Thread Control Block (TCB)
Threads in Java
Lightweight processes are called threads. Creating a new thread needs fewer resources than creating a new process. Similar to processes, a thread provides an execution environment for our code.
A process contains one or more threads. Threads share the resources of a process. The threads within a process share all the information of a process except the information directly related to the execution.
The execution state of thread is moved out of the PCB and is known as Thread Control Block (TCB). Each thread has their own TCB.
In above image PC is a program counter and SP is stack pointer.
Thread in a process, can execute different parts of a program code, at the same time. They have independent program counters, registers.
Process start with a single main thread. The main thread then create new threads using system call (fork). New threads can also create other new threads using the fork system call. Threads in Java can be created in a similar fashion.
There is no existence of a thread without a process.
States of a Thread in Java
Java thread can have following states
NEW: In this state a thread has not yet started.
RUNNABLE: In this state a thread is executing in the Java virtual machine.
BLOCKED: In this state a thread is blocked and waiting for a monitor lock.
WAITING: In this state a thread is waiting indefinitely for another thread to perform a particular action.
TIMED_WAITING: In this state a thread is waiting for another thread to perform an action for up to a specified waiting time.
TERMINATED: In this state a thread has exited.
Multithreading in Java can achieved using various threads in our program. We will see more details about threads in java in following articles.
Process and Threads (Operating System)