Join Ketkee Aryamane for an in-depth discussion in this video What is concurrency?, part of Java EE: Concurrency and Multithreading.
- [Narrator] This course talks about JAVA Enterprise Edition Concurrency utilities. For that, we will have to take a look at some of the Standard Edition Concurrency APIs. But, even before that, let us understand what is "concurrency". You must have heard of these terms interchangeably, concurrency, parallelism, and asynchronous tasks. Let's understand each one of them. When you talk about concurrency, it's a machine or a software application executing multiple tasks for you.
These tasks could be functions, or they could be parts of a program, or a completely different program. And, in concurrency, all of these tasks will have overlapped time periods. On the other hand, when you talk about parallelism, it means that parts of a task, or several tasks are running together exactly at the same time. And, there's additionally another term, where we say application is capable of executing asynchronous tasks. Okay, so what is an asynchronous task? Let's take a look at it.
Let's say, I have an application which is trying to communicate with the RESTful API, or a database. It churns out a lot of data, and then readies it to take it back to the client to display on the HTML inside the user's browser. Now, the HTML is the sole medium by which the user interacts with the system. So, whatever action is performed by the user on the HTML, let's say like button click, or maybe mouse hover, it sends a request to the application for processing and then comes back with a response which immediately gets updated on the HTML.
Now, this processing was such that since the response got updated on the HTML immediately, the user kept waiting all that while. This kind of a call is a "real time call", or as we say, "synchronous call". However, there are certain jobs in your application which do not require user interference at all. These kinds of tasks also take a longer time to execute in your application. For example, if I want to send out an email to all the users of my application.
Or, let's say, I want to do certain cleanup and maintenance activities for my application. Or, let's say, I want to log in a lot of data for my application. These tasks do not require user interference, which means that there is no point keeping the user waiting for these tasks to get completed. These kind of tasks are called "asynchronous tasks". Asynchronous calls in your application always take a callback reference, so that, when they get completed, the callback function executes and then the response gets updated to the HTML.
Now, since the response gets updated at a later point in time, the user does not have to wait for it, and can keep doing other things on the application. Now, when you talk about asynchronous tasks, they do give you an impression of concurrency, because there are jobs running within your system whilst the user is trying to do something else through the user interface. When we talk about concurrency with the Java platform, there are two sets of APIs that we have with us. One is under the Java Standard Edition, and the other is a part of the Java Enterprise Edition.
Concurrency programming, particularly when stated in Java, is via multithreading. And, we will talk about multithreading in a minute.
- What is concurrency?
- Processes and threads in Java SE
- Java EE concurrency APIs
- Working with the ManagedExecutorService, ManagedScheduledExecutorService, ManagedThreadFactory, and ContextService APIs
- Defining data access object (DAO) classes
- Adding methods to DAO classes
- Writing REST classes
- Creating custom thread factories
- Creating contextual proxies
- JTA transactions and concurrency