The course is part of this learning path
In this course, we will learn the concepts of Java EE 7 with a focus on Concurrency Utilities with Threads, Semaphore, Phaser and other methods, and Transactions.
- Concurrency Utilities
- Anyone looking to get Oracle Java Certification
- Those who want to improve Java 7 EE knowledge
- Java developers
- Have at least 2 years of Java development experience
Hello there. In this lesson, we will continue to talk about concurrency in java. So, let's start. Concurrent collections. The java.util.concurrent package provides a number of classes that are thread-safe equivalents of the ones provided in the collections framework classes in the java.util package. For example, java.util.concurrent.concurrentHashmap is a concurrent equivalent to java.util.Hashmap. The main difference between these two containers is that you need to explicitly synchronize insertions and deletions with Hashmap whereas such synchronization is built into the concurrent HashMap. If you know how to use HashMap, you know how to use ConcurrentHashMap implicitly. BlockingQueue. This interface extends the queue interface. In blockingQueue, if the queue is empty, it waits i.e. blocks for an element to be inserted, and if the queue is full, it waits for an element to be removed from the queue. ArrayBlockingQueue. This class provides a fixed size array based implementation of the BlockingQueue interface. LinkedBlockingQueue.
This class provides a linked list based implementation of the BlockingQueue interface. DelayQueue. This class implements BlockingQueue and consists of elements that are of type delayed. An element can be retrieved from this queue only after its delay period. PriorityBlockingQueue. Equivalent to java.util.priorityqueue, but implements the BlockingQueue interface. SynchronousQueue. This class implements BlockingQueue. In this container, each insert by a thread waits, blocks for a corresponding remove by another thread and vice versa. LinkedbBockingDeque. This class implements BlockingDeque where insert and remove operations could block, uses a link list for implementation. ConcurrentHashMap. Analogous to HashTable, but with safe concurrent access and updates. ConcurrentSkipListMap. Analogous to TreeMap, but provides safe concurrent access and updates. ConcurrentSkipListSet. Analogous to TreeSet, but provides safe concurrent access and updates. CopyOnWriteArrayList; similar to ArrayList, but provide safe concurrent access.
When the array is updated, it creates a fresh copy of the underlying array. CopyOnWriteArraySet. A set implementation but provides safe concurrent access and is implemented using CopyOnWriteArrayList. When the container is updated, it creates a fresh copy of the underlying array. Now we will make two examples. These show how a concurrent version differs from its non-concurrent version. Assume that you have a PriorityQueue object shared by two threads. Assume that one thread inserts an element into the PriorityQueue and the other thread removes an element. If the threads are scheduled such that the inserting an element occurs before the removing the element, there is no problem. However, if the first thread attempts to remove an element before the second thread inserts an element, you get into trouble. Let's move on to the Eclipse. We'll create a java class in this project. Right click to show the context menu and choose the menu item 'New and class'.
We must specify the class name. And lastly, I will check the box 'public static void main' to let eclipse create the main method. Simple PriorityQueue example. Here, we create two threads in which one thread inserts an element and another thread removes an element from the PriorityQueue. Spawn a thread that removes an element from the PriorityQueue. Use remove method in PriorityQueue to remove the element if available. Spawn a thread that inserts an element into the PriorityQueue. Insert integer value 10 as an entry into the PriorityQueue. Now, let's run the application and test it. The output is like this. This output indicates that the first thread attempted removing an element from an empty PriorityQueue, and hence it results in a no such element exception. However, consider a slight modification of this program that uses PriorityBlockingQueue instead of PriorityQueue. Now, let's create a second class in the same package. I right click on 'package' and select 'new class option'.
We must specify the class name. I click the 'main method' box and click the 'finish' button to create my class. Illustrates the use of PriorityBlockingQueue; In this case, if there is no element available in the PriorityQueue, the thread calling take method will block until another thread inserts an element. Spawn a thread that removes an element from the PriorityBlockingQueue. Use take instead of remove. Note that take blocks whereas remove doesn't block. As you can see, the compiler gives a compilation error because we have to put it in the try and catch block. Here, I choose the surround with try catch option. As you can see, the compilation error has disappeared. Spawn a thread that inserts an element into the PriorityBlockingQueue. Add an element with value 10 to the PriorityQueue. Now, let's run the application and test it.
The output is like this. This program will not result in a crash as the previous case. This is because the take method will block until an element gets inserted by another thread. Once inserted, the take method will return that value. In other words, if you're using a PriorityQueue object, you need to synchronize the threads such that an insertion of an element always occurs before removing an element. However, in PriorityBlockingQueue, the order does not matter. And no matter which operation, insertion or removal of an element is invoked first, the program works correctly. In this way, concurrent collections provides support for safe use of collections in the context of multiple threads without the need for you to perform explicit synchronization operations. So, that's it. Hope to see you in our next lesson. Have a nice day.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.