![]() ![]() ![]() Methods of Queue interfaceīoolean add(E e): This method adds the specified element at the end of Queue. If you try to add the element of the type not specified then you will get compilation error, this brings the safety in our program and makes it less error prone. As you can see I have specified the type of the Queue as String using Generics, so that it accepts only String elements. In the above example, I have used Generics, this helps us to specify the type of the element that we are going to insert into the collection. * however it returns null if the Queue is empty * peek() method - it works same as element() method, ![]() * poll() method - this removes and returns the * element() method - this returns the head of the * this would remove the first element from the Queue ![]() * We can remove element from Queue using remove() method, * interface, we can create instance of LinkedList or * We cannot create instance of a Queue as it is an Queue q2 = new PriorityQueue() Java Queue Example import java.util.* Queue is an interface so we cannot instantiate it, rather we create instance of LinkedList or PriorityQueue and assign it to the Queue like this: Queue q1 = new LinkedList() Queue interface in Java collections has two implementation: LinkedList and PriorityQueue, these two classes implements Queue interface. The concept here is similar to the queue we see in our daily life, for example, when a new iPhone launches we stand in a queue outside the apple store, whoever is added to the queue has to stand at the end of it and persons are served on the basis of FIFO (First In First Out), The one who gets the iPhone is removed from the beginning of the queue. The consumer will wait on the countDownLatch using the await() method.A Queue is designed in such a way so that the elements added to it are placed at the end of Queue and removed from the beginning of Queue. Integer producedElement = ThreadLocalRandom The producer will save a random integer to the sharedState variable, and execute the countDown() method on the countDownLatch, signaling to the consumer that it can fetch a value from the sharedState: We will define a sharedState variable and a CountDownLatch that will be used for coordinating processing: ExecutorService executor = Executors.newFixedThreadPool(2) ĪtomicInteger sharedState = new AtomicInteger() ĬountDownLatch countDownLatch = new CountDownLatch(1) We will use the CountDownLatch to coordinate those two threads, to prevent a situation when the consumer accesses a value of a shared variable that was not set yet. Next, the consumer thread will fetch a value from a shared variable. Let's say that we have two threads – a producer and a consumer – and when the producer is setting a value of a shared variable, we want to signal that fact to the consumer thread. Java queue code#To see why the SynchronousQueue can be so useful, we will implement a logic using a shared variable between two threads and next, we will rewrite that logic using SynchronousQueue making our code a lot simpler and more readable. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |