BlockingArrayQueue.java. Page 1

Similar documents
CMSC 132, Object-Oriented Programming II Summer Lecture 9:

CMSC 132: Object-Oriented Programming II. Stack and Queue

infix expressions (review)

Stacks and Queues. David Greenstein Monta Vista

Introduction to Computer Science II (ITI 1121) Final Examination

More Data Structures (Part 1) Stacks

Queues. Stacks and Queues

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Producer/Consumer CSCI 201 Principles of Software Development

Synchronized Methods of Old Versions of Java

Basic Data Structures

Basic Data Structures 1 / 24

CSC 1052 Algorithms & Data Structures II: Queues

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

Chapter 18: Stacks And Queues

CS 351 Design of Large Programs Threads and Concurrency

Introduction to Computing II (ITI 1121) Final Examination

ITI Introduction to Computing II

Collections and Iterators. Collections

Data Structures G5029

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

csci 210: Data Structures Stacks and Queues

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Recursive Objects. Singly Linked List (Part 2)

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Chapter 18: Stacks And Queues

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

CSC 1052 Algorithms & Data Structures II: Linked Queues

Multi-Threaded Programming Design CSCI 201 Principles of Software Development

Queues Fall 2018 Margaret Reid-Miller

Software Construction

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Announcements Queues. Queues

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Stack and Queue. Stack:

Search EECS /57

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Linked Structures. See Section 3.2 of the text.

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

Need to access each element of a list. Use index to access an element of the list. It s quadratic, while:

1. Introduction. Lecture Content

Queues. Data Structures and Design with Java and JUnit Rick Mercer 18-1

Stacks and Queues III

The Java Collections Framework. Chapters 7.5

Object Oriented Software Design

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Midterm #1. CMSC 433: Programming Language Technologies and Paradigms. October 14, 2013

2IP15 Programming Methods

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CSEN 301 Data Structures and Algorithms

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

Computer Science 62. Midterm Examination

JCF: user defined collections

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Objects and Iterators

Lecture 4. The Java Collections Framework

Interfaces & Generics

Programming II (CS300)

Programming II (CS300)

Queue rear tail front head FIFO

Java Review: Objects

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

COMP 250. inheritance (cont.) interfaces abstract classes

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

JAVA NOTES DATA STRUCTURES AND ALGORITHMS

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

CE204 Data Structures and Algorithms Part 2

Lecture Notes Chapter #9 Summery. Inheritance

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

CSCD 326 Data Structures I Queues

A queue is a linear collection whose elements are added on one end and removed from the other

Class 26: Linked Lists

CMSC 341. Linked Lists. Textbook Section 3.5

COMP 322: Fundamentals of Parallel Programming

CSCI 200 Lab 11 A Heap-Based Priority Queue

CSE 143 SAMPLE MIDTERM

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

List ADT. Announcements. The List interface. Implementing the List ADT

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Implementing Lists, Stacks, Queues, and Priority Queues

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Abstract Classes and Interfaces

An Introduction to Queues With Examples in C++

Announcements. CS18000: Problem Solving And Object-Oriented Programming

חומר עזר לבחינה מבוא למדעי המחשב

Transcription:

1 //BlockingArrayQueue.java 2 //Template blocking queue class, implemented with emulated circular array. 3 //Programmer: Randy Miller 4 //Last modification: April 13, 2015 5 6 7 8 import java.util.arraylist; 9 import java.util.collection; 10 import java.util.iterator; 11 import java.util.nosuchelementexception; 12 import java.util.concurrent.locks.lock; 13 import java.util.concurrent.locks.reentrantlock; 14 import java.util.concurrent.locks.condition; 15 16 public class BlockingArrayQueue<T> /*implements java.util.queue<t> (not quite, yet: a few more general methods 17 from the Collection superinterface remain unimplemented, and a few methods 18 implemented here under another name need to be expressly defined as the 19 delegates of required methods, or else renamed)*/ 20 implements Cloneable 21 { 22 private boolean empty = true, 23 full = false; 24 private int capacity, //maximum queue size 25 head = 0, //index of the next element to be dequeued 26 tail = 0; //index of the next element to be enqueued 27 private ArrayList<T> queue; 28 private Lock lock; 29 private Condition queuenotempty, 30 queuenotfull; 31 32 //construction from an initial collection of queue members 33 public BlockingArrayQueue(Collection<? extends T> queue, int capacity) 34 { 35 this.queue = new ArrayList<T>(queue); 36 this.capacity = capacity; 37 if (queue.size() > capacity) 38 { 39 for (int i = capacity; i < queue.size(); i++) 40 this.queue.remove(i); 41 this.queue.trimtosize(); 42 } 43 this.tail = (queue.size() < capacity? queue.size() : /*capacity*/0); 44 if (queue.size() > 0) 45 { Page 1

46 empty = false; 47 full = (tail == 0? true : false); 48 } 49 lock = new ReentrantLock(); 50 queuenotempty = lock.newcondition(); 51 queuenotfull = lock.newcondition(); 52 } 53 54 //construction from queue capacity only 55 public BlockingArrayQueue(int capacity) 56 { 57 this.queue = new ArrayList<T>(capacity); 58 this.capacity = capacity; 59 lock = new ReentrantLock(); 60 queuenotempty = lock.newcondition(); 61 queuenotfull = lock.newcondition(); 62 } 63 64 //default constructor, with default capacity of 10 65 public BlockingArrayQueue() 66 { 67 this(10); 68 } 69 70 public int getcapacity() { return capacity; } 71 72 public synchronized int getsize() { return (tail!= head? ((tail + capacity - head) % capacity) : 73 (full? capacity : 0)); } 74 75 public synchronized boolean isfull() { return full; } 76 77 public synchronized boolean isempty() { return empty; } 78 79 //a method of the java.util.queue interface 80 public synchronized boolean add(t member) 81 { 82 if (isfull()) 83 throw new IllegalStateException(); 84 else 85 { 86 enqueue(member); 87 return true; 88 } 89 } 90 Page 2

91 //a method of the java.util.queue interface 92 public synchronized boolean offer(t member) 93 { 94 if (isfull()) 95 return false; 96 else 97 { 98 enqueue(member); 99 return true; 100 } 101 } 102 103 //base blocking queue put() implementation 104 public void enqueue(t member) 105 { 106 lock.lock(); 107 try 108 { 109 while (isfull()) 110 { 111 try 112 { 113 queuenotfull.await(); 114 } 115 catch (InterruptedException exception) 116 { 117 Thread.currentThread().interrupt(); 118 } 119 } 120 if (queue.size() < capacity) 121 queue.add(tail, member); 122 else 123 queue.set(tail, member); 124 tail = (tail + 1) % capacity; 125 if (tail == head) full = true; 126 empty = false; 127 queuenotempty.signalall(); 128 } 129 finally 130 { 131 lock.unlock(); 132 } 133 } 134 135 //a method of the java.util.queue interface BlockingArrayQueue.java Page 3

136 public synchronized T element() 137 { 138 if (isempty()) 139 throw new NoSuchElementException(); 140 else 141 { 142 return dequeue(false); 143 } 144 } 145 146 //a method of the java.util.queue interface 147 public synchronized T peek() 148 { 149 if (isempty()) 150 return null; 151 else 152 { 153 return dequeue(false); 154 } 155 } 156 157 //function of the java.util.queue interface 158 public synchronized T remove() 159 { 160 if (isempty()) 161 throw new NoSuchElementException(); 162 else 163 { 164 return dequeue(true); 165 } 166 } 167 168 //a method of the java.util.queue interface 169 public synchronized T poll() 170 { 171 if (isempty()) 172 return null; 173 else 174 { 175 return dequeue(true); 176 } 177 } 178 179 //base blocking queue get() implementation 180 public T dequeue(boolean notjustpeeking) Page 4

181 { 182 T fifo = null; 183 lock.lock(); 184 try 185 { 186 while (isempty()) 187 { 188 try 189 { 190 queuenotempty.await(); 191 } 192 catch (InterruptedException exception) 193 { 194 Thread.currentThread().interrupt(); 195 } 196 } 197 fifo = queue.get(head); 198 if (notjustpeeking) 199 { 200 head = (head + 1) % capacity; 201 if (head == tail) empty = true; 202 full = false; 203 queuenotfull.signalall(); 204 } 205 } 206 finally 207 { 208 lock.unlock(); 209 } 210 return fifo; 211 } 212 213 @Override 214 @SuppressWarnings("unchecked") 215 public synchronized BlockingArrayQueue<T> clone() 216 { 217 BlockingArrayQueue<T> clone; 218 219 try 220 { 221 clone = (BlockingArrayQueue<T>)super.clone(); 222 } 223 catch (CloneNotSupportedException e) { throw new Error(); } //this won't ever happen 224 225 /*would be better to individually clone() the members of the ArrayList, but Object::clone() is Page 5

226 a protected method and I can't see how to bring it off. So if T does not wrap a primitive 227 we will just get a new ArrayList populated with new references to the same old objects. 228 Better than nothing I suppose.*/ 229 clone.queue = new ArrayList<T>(this.queue); 230 231 return clone; 232 } 233 234 /*It's not usually a good idea to lock an object for time-consuming IO operations, but 235 I was unable to see how to define a publicly accessible override of the Object::clone() 236 method for the unknown type T that is the class instantiated by members of the queue, 237 and so was unable to create a threadsafe clone of the entire containing queue for IO. 238 239 I did what I could above, and am now limiting this method's lock on the object to its 240 call to this.clone(). At least the efficiency problem is minimized*/ 241 242 @Override 243 public String tostring() 244 { 245 BlockingArrayQueue<T> clone= this.clone(); 246 247 StringBuilder textbuffer = new StringBuilder(); 248 249 textbuffer.append(string.format("queued elements, ordered from the head of the line to the end:%n")); 250 251 if (!isempty()) 252 { 253 for (int i = 0; i < getsize(); i++) 254 { 255 256 String memberstring = (clone.queue).get((head + i) % capacity).tostring(); 257 if (memberstring.length() > 15) 258 memberstring = memberstring.substring(0, 15); 259 textbuffer.append(string.format("element %3d: %-15s%n", i + 1, memberstring)); 260 } 261 } 262 else 263 textbuffer.append("(queue is empty)\n"); 264 265 return textbuffer.tostring(); 266 } 267 268 //output the members of the queue, oldest first 269 public void print() 270 { Page 6

271 System.out.print(toString()); 272 } 273 274 /*The following implementation of an iterator for this queue is not threadsafe or even weakly consistent. 275 (by the time that a (mutable) element of the cloned queue is accessed another thread may have altered it 276 through the use of a different reference.) Where multi-threaded access to the queue is possible the queue 277 must be locked throughout the iteration*/ 278 279 public Iterator<T> iterator() 280 { 281 return new BlockingArrayQueueIterator<T>(); 282 } 283 284 private class BlockingArrayQueueIterator<E> implements Iterator<E> 285 { 286 int index; 287 boolean firstcycleofafullqueueinprogress = false; 288 BlockingArrayQueue<E> clone; 289 290 @SuppressWarnings("unchecked") 291 public BlockingArrayQueueIterator() 292 { 293 clone = (BlockingArrayQueue<E>)BlockingArrayQueue.this.clone(); 294 index = clone.head; 295 if (clone.isfull()) 296 firstcycleofafullqueueinprogress = true; 297 } 298 299 public boolean hasnext() 300 { 301 return (!isempty() && (index!= clone.tail (index == clone.head && firstcycleofafullqueueinprogress))); 302 } 303 304 305 public E next() 306 { 307 E element; 308 309 if (hasnext()) 310 { 311 element = clone.queue.get(index); 312 index = (index + 1) % capacity; 313 if (index == tail && firstcycleofafullqueueinprogress) 314 firstcycleofafullqueueinprogress = false; 315 } Page 7

316 else 317 throw new NoSuchElementException(); 318 319 return element; 320 } 321 } 322 } 323 Page 8