By Md. Sabuj Sarker | 9/25/2017 | General |Beginners

Understanding Concurrency in Java

Understanding Concurrency in Java

At the heart of every computer lies a microprocessor. A microprocessor executes the commands given to it one by one, unless it has more than one core. Let's imagine for a moment that we are living in a world where every microprocessor is single core as was the case some years ago. Even today there are some computer systems that run on a single core microprocessor. Now, think about the following questions for a moment:

  • How can your computer run multiple programs at the same time?
  • How can an operating system multitask when the CPU can only run one command at a time?
  • How can your web browser load multiple images in parallel?

Now, if you are done with time traveling, let’s jump back to today. You have a multi-core CPU attached to your computer's motherboard. It can run more than one command at a time by way of multiple cores. That also means that no core can execute more than one command at the same time. Your desktops, laptops, and smartphones may have two or more cores inside their CPUs. Octacore, Hexacore systems are widely available to the general public. So, let's say that you have 16 cores inside your CPU. Open the task manager to see how many processes are running inside your OS. There are more than 16 processes running. Ask yourself twice:

  • If one core can run one command at a time then how are all those processes are running simultaneously?

Again, many single processes or applications are performing multiple tasks simultaneously. So, does how all this happen? The operating system schedules processes and threads to give every task a chance to run itself. It's your operating system's responsibility to run multiple program at the same time. We are not going to discuss how operating systems schedule the processes and threads. We are concerned about how we can have multiple tasks inside one application.

Concurrency is the ability to perform multiple tasks at the same time. But at the same time doesn't mean that the CPU core executes the command at the same time—it means that the tasks are apparently running at the same time. Java provides great mechanisms for concurrency. The most basic building block of concurrency in Java is the thread. The most important class for it is the Thread class.

Process

To an ordinary computer user, there are only applications. But from the view of a programmer, there are only processes. Applications are abstractions and an application can have one or more process. Some processes have windows or UIs and some don't. Every process has separate resources. A process has its own private virtual memory space. All other resources that are owned by a process are separate from the rest of the system. Usually if the resources are not released by the process, they are released on the process exit or destruction. When different processes need to communicate with each other they do so with the help of an Inter Process Communication mechanism provided by the operating system. Inter Process Communication is less efficient than how threads communicate.

In reality, the process does not run or execute—threads run or execute. Every process is created with a primary or main thread and processes are nothing but a container for threads and resources.

In Java we do not think about process that much. Almost every concurrent application in Java uses threads. We can work with process with the help of the ProcessBuilder class.

Thread

Threads are usually referred to as lightweight processes. Creating threads takes less time compared to processes and does not need to have a private set of memory. It shares the memory or object already existent inside the host process. What it needs exclusively is a runtime stack. For all their goodness, threads are always the preferred method of concurrency in Java. Unlike some other modern programming languages, Java does not have Global Interpreter Lock (GIL) or such things that makes multithreading in Java more speedy—though it needs caution.

With the help of threads, Java can share classes, objects, and all other resources. Java provides abstraction over the host operating system. So, the programmer does not need to worry about the native mechanism of creating, running, and destroying threads.

Depending on how many cores you have in your system, the threads are run on different cores to provide maximum performance.

Synchronization

In most of the languages synchronization is one of the biggest problem of developing multithreaded applications. You need to manage, acquire, and release locks manually in code to share resources that have the risk of being improperly modified or updated resulting in a corrupt result in runtime. Java provides the keyword synchronized for this. By placing this keyword on methods or code blocks you can forget about everything else with synchronized access to a method or a block of block.

Synchronization should be used with care otherwise your application will suffer from performance issue. You should not place all the code inside the synchronized method or code block in fear of data being modified by multiple thread at the same time. You should only place the critical section of the code that must be synchronized and you should place the objects inside the synchronized block or method to safely share objects. You must not use a synchronized block here and there without care. The more you use it the higher the risk of bugs, so be careful in using it.

Concurrency, Parallelism and Asynchronous

Many programmers confuse concurrency, parallelism and synchronicity. They are not the same. It is not possible to discuss them all here and would need separate articles on each, which we will hopefully have here in the near future. I am only trying to clarify parallelism in short here.

When two parts of a program can run independently and run at the very same time, we call this behavior parallelism. If two instructions are running on two different cores then it is running in parallel. One instruction does not depend on the other to be finished, as they are running on two different cores. This is parallelism. You have two programs running on two different computer, thus having two different CPUs, and are running in parallel. When a giant task is divided into small parts running on different CPUs and the results are joined at the end this is running in parallel. Graphics processors or GPUs have multiple cores and thus they can distribute graphics processing to different CPUs to process them faster and join the result at the end. This too is parallelism.

Concurrent may be parallel and may not be. Asynchronous applications do not run in parallel but they do not waste time. In our program, often we have to wait for some result. Instead of waiting we can run another block of code at that time and when the result arrives we can start processing that.

Conclusion

Concurrency is not the answer to everything. You may code your application such that it works in an asynchronous manner. For example, you can create an event loop like GUI applications do that has one primary thread and avoids creating more thread if possible. In this way you can use your resources efficiently. The more threads you create the more extra memory your program consumes. Every separate runtime stack is an extra overhead for your system. Also, for many tasks you can use RxJava.

This article was a theoretical discussion on concurrency in Java. I am going to write more articles for you in which I will cover various things in details and provide you with code examples.

 

Need more Java? Learn how to use threads in Java and be sure to stop by the homepage to search and compare the best SDKs, APIs, and code libraries

By Md. Sabuj Sarker | 9/25/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now