SAFE Logo
CodeMatch Detailed Report
Version: 5.6.0 | Date: 06/18/12 | Time: 19:09:04

SCORE

SETTINGS
Compare file 1: C:\Users\Evan\Desktop\Java2SE5.0\src\java\util\concurrent\LinkedBlockingQueue.java
To file 2: C:\Users\Evan\Desktop\Android2.2Froyo\java\util\concurrent\LinkedBlockingQueue.java
Links to results: Matching Statements
Matching Comments and Strings
Matching Instruction Sequences
Matching Identifiers
Partially Matching Identifiers
Score
RESULTS
Matching Statements
File1 Line# File2 Line# Statement
87package java.util.concurrent
98import java.util.concurrent.atomic.*
109import java.util.concurrent.locks.*
1110import java.util.*
4544public class LinkedBlockingQueue<E> extends AbstractQueue<E>
4645implements BlockingQueue<E>, java.io.Serializable {
4746private static final long serialVersionUID = -6903933977591709194L
6665static class Node<E> {
6867volatile E item
6968Node<E> next
7069Node(E x) { item = x
7473private final int capacity
7776private final AtomicInteger count = new AtomicInteger(0)
8079private transient Node<E> head
8382private transient Node<E> last
8685private final ReentrantLock takeLock = new ReentrantLock()
8988private final Condition notEmpty = takeLock.newCondition()
9291private final ReentrantLock putLock = new ReentrantLock()
9594private final Condition notFull = putLock.newCondition()
101100private void signalNotEmpty() {
102
354
382
416
437
101
354
382
416
437
final ReentrantLock takeLock = this.takeLock
103
149
417
438
617
636
658
102
150
417
438
694
713
735
takeLock.lock()
105
361
368
390
398
423
104
361
368
390
398
423
notEmpty.signal()
107
156
370
403
426
446
623
647
676
106
157
370
403
426
446
700
724
755
takeLock.unlock()
114113private void signalNotFull() {
115
238
288
332
114
240
286
332
final ReentrantLock putLock = this.putLock
116
148
333
616
635
657
115
149
333
693
712
734
putLock.lock()
118
255
261
297
305
339
117
257
263
295
303
339
notFull.signal()
120
157
263
310
342
624
648
677
119
158
265
308
342
701
725
756
putLock.unlock()
128127private void insert(E x) {
129128last = last.next = new Node<E>(x)
136135private E extract() {
137
440
440Node<E> first = head.next
138139head = first
139140E x = first.item
140141first.item = null
141
374
407
430
645
142
374
407
430
722
return x
147148private void fullyLock() {
155156private void fullyUnlock() {
165166public LinkedBlockingQueue() {
166
192
167
194
this(Integer.MAX_VALUE)
176177public LinkedBlockingQueue(int capacity) {
177178if (capacity <= 0) throw new IllegalArgumentException()
178179this.capacity = capacity
179
720
180
799
last = head = new Node<E>(null)
191193public LinkedBlockingQueue(Collection<? extends E> c) {
205207public int size() {
206208return count.get()
222224public int remainingCapacity() {
223225return capacity - count.get()
237
287
331
352
379
415
239
285
331
352
379
415
int c = -1
239
289
328
353
381
411
241
287
328
353
381
411
final AtomicInteger count = this.count
240
290
242
288
putLock.lockInterruptibly()
252254while (count.get() == capacity)
253255notFull.await()
254
304
360
397
256
302
360
397
} catch (InterruptedException ie) {
256
306
362
399
258
304
362
399
throw ie
259
295
337
261
293
337
c = count.getAndIncrement()
260
296
338
262
294
338
if (c + 1 < capacity)
265
312
344
267
310
344
if (c == 0)
266
313
345
268
311
345
signalNotEmpty()
283281throws InterruptedException {
286
380
284
380
long nanos = unit.toNanos(timeout)
293
335
291
335
if (count.get() < capacity) {
300
393
298
393
if (nanos <= 0)
303301nanos = notFull.awaitNanos(nanos)
329329if (count.get() == capacity)
346346return c >= 0
350350public E take() throws InterruptedException {
351351E x
355
383
355
383
takeLock.lockInterruptibly()
358358while (count.get() == 0)
359359notEmpty.await()
365
387
420
365
387
420
x = extract()
366
388
421
366
388
421
c = count.getAndDecrement()
367
389
422
367
389
422
if (c > 1)
372
405
428
672
372
405
428
751
if (c == capacity)
373
406
429
373
406
429
signalNotFull()
377377public E poll(long timeout, TimeUnit unit) throws InterruptedException {
378
414
378
414
E x = null
386
419
386
419
if (count.get() > 0) {
396396nanos = notEmpty.awaitNanos(nanos)
410410public E poll() {
412
435
412
435
if (count.get() == 0)
434434public E peek() {
441441if (first == null)
444444return first.item
454461public boolean remove(Object o) {
455462if (o == null) return false
456463boolean removed = false
457
482
496
513
526
542
568
693
464
504
554
573
586
610
642
772
fullyLock()
459
662
466
739
Node<E> trail = head
460
571
663
467
645
740
Node<E> p = head.next
461468while (p != null) {
462469if (o.equals(p.item)) {
463470removed = true
466
665
473
742
trail = p
467
575
666
474
649
743
p = p.next
469476if (removed) {
470
555
574
669
477
625
648
746
p.item = null
471
670
478
747
trail.next = p.next
472481if (count.getAndDecrement() == capacity)
473
530
547
581
673
482
592
617
658
752
notFull.signalAll()
476
491
508
517
532
549
585
705
485
513
568
577
594
619
662
784
fullyUnlock()
478487return removed
481503public Object[] toArray() {
484
498
506
556
int size = count.get()
485507Object[] a = new Object[size]
486
503
508
561
int k = 0
487
699
509
778
for (Node<E> p = head.next
487
504
553
699
509
562
623
778
p != null
487
504
699
509
562
778
p = p.next)
488510a[k++] = p.item
489
506
511
566
return a
495553public <T> T[] toArray(T[] a) {
499557if (a.length < size)
500558a = (T[])java.lang.reflect.Array.newInstance
501559(a.getClass().getComponentType(), size)
504562for (Node p = head.next
505563a[k++] = (T)p.item
512572public String toString() {
515575return super.toString()
525585public void clear() {
528
545
588
613
head.next = null
529
546
591
616
if (count.getAndSet(0) == capacity)
536604public int drainTo(Collection<? super E> c) {
537
562
605
638
if (c == null)
538
563
606
639
throw new NullPointerException()
539
564
607
640
if (c == this)
540
565
608
641
throw new IllegalArgumentException()
544612first = head.next
552
570
622
644
int n = 0
553623for (Node<E> p = first
553623p = p.next) {
554
573
624
647
c.add(p.item)
556
576
626
650
++n
558
583
628
660
return n
561637public int drainTo(Collection<? super E> c, int maxElements) {
572646while (p != null && n < maxElements) {
578652if (n != 0) {
579653head.next = p
580657if (count.getAndAdd(-n) == capacity)
599676public Iterator<E> iterator() {
600677return new Itr()
603680private class Itr implements Iterator<E> {
609686private Node<E> current
610687private Node<E> lastRet
611688private E currentElement
613690Itr() {
614
633
655
691
710
732
final ReentrantLock putLock = LinkedBlockingQueue.this.putLock
615
634
656
692
711
733
final ReentrantLock takeLock = LinkedBlockingQueue.this.takeLock
619696current = head.next
620
643
697
720
if (current != null)
621
644
698
721
currentElement = current.item
628705public boolean hasNext() {
629706return current != null
632709public E next() {
638715if (current == null)
639716throw new NoSuchElementException()
640717E x = currentElement
641718lastRet = current
642719current = current.next
652729public void remove() {
653730if (lastRet == null)
654731throw new IllegalStateException()
660737Node<E> node = lastRet
661738lastRet = null
664741while (p != null && p != node) {
668745if (p == node) {
671750int c = count.getAndDecrement()
690769private void writeObject(java.io.ObjectOutputStream s)
691770throws java.io.IOException {
696775s.defaultWriteObject()
700779s.writeObject(p.item)
703782s.writeObject(null)
714793private void readObject(java.io.ObjectInputStream s)
715794throws java.io.IOException, ClassNotFoundException {
717796s.defaultReadObject()
719798count.set(0)
724803E item = (E)s.readObject()
725804if (item == null)
727806add(item)

to top

Matching Comments and Strings
File1 Line# File2 Line# Comment/String
1417* An optionally-bounded {@linkplain BlockingQueue blocking queue} based on
1518* linked nodes.
1619* This queue orders elements FIFO (first-in-first-out).
1720* The <em>head</em> of the queue is that element that has been on the
1821* queue the longest time.
1922* The <em>tail</em> of the queue is that element that has been on the
2023* queue the shortest time. New elements
2124* are inserted at the tail of the queue, and the queue retrieval
2225* operations obtain elements at the head of the queue.
2326* Linked queues typically have higher throughput than array-based queues but
2427* less predictable performance in most concurrent applications.
2629* <p> The optional capacity bound constructor argument serves as a
2730* way to prevent excessive queue expansion. The capacity, if unspecified,
2831* is equal to {@link Integer#MAX_VALUE}. Linked nodes are
2932* dynamically created upon each insertion unless this would bring the
3033* queue above capacity.
3235* <p>This class and its iterator implement all of the
3336* <em>optional</em> methods of the {@link Collection} and {@link
3437* Iterator} interfaces.
4039* @since 1.5
4140* @author Doug Lea
4241* @param <E> the type of elements held in this collection
5049* A variant of the "two lock queue" algorithm. The putLock gates
5150* entry to put (and offer), and has an associated condition for
5251* waiting puts. Similarly for the takeLock. The "count" field
5352* that they both rely on is maintained as an atomic to avoid
5453* needing to get both locks in most cases. Also, to minimize need
5554* for puts to get takeLock and vice-versa, cascading notifies are
5655* used. When a put notices that it has enabled at least one take,
5756* it signals taker. That taker in turn signals others if more
5857* items have been entered since the signal. And symmetrically for
5958* takes signalling puts. Operations such as remove(Object) and
6059* iterators acquire both locks.
6463* Linked list node class
6766* The item, volatile to ensure barrier separating write and read
7372* The capacity bound, or Integer.MAX_VALUE if none
7675* Current number of elements
7978* Head of linked list
8281* Tail of linked list
8584* Lock held by take, poll, etc
8887* Wait queue for waiting takes
9190* Lock held by put, offer, etc
9493* Wait queue for waiting puts
9998* otherwise ordinarily lock takeLock.)
126125* @param x the item
134133* @return the node
145146* Lock to prevent both puts and takes.
153154* Unlock to allow both puts and takes.
162
183
163
184
* Creates a <tt>LinkedBlockingQueue</tt> with a capacity of
163164* {@link Integer#MAX_VALUE}.
170171* Creates a <tt>LinkedBlockingQueue</tt> with the given (fixed) capacity.
173174* @throws IllegalArgumentException if <tt>capacity</tt> is not greater
184185* {@link Integer#MAX_VALUE}, initially containing the elements of the
185186* given collection,
186187* added in traversal order of the collection's iterator.
187189* @param c the collection of elements to initially contain
198200this doc comment is overridden to remove the reference to collections
199201greater in size than Integer.MAX_VALUE
201203* Returns the number of elements in this queue.
209211this doc comment is a modified copy of the inherited doc comment,
210212without the reference to unlimited queues.
214216* blocking. This is always equal to the initial capacity of this queue
215217* less the current <tt>size</tt> of this queue.
228230* necessary for space to become available.
235237Note: convention in all put/take/etc is to preset
236238local var holding count negative to indicate failure unless set.
243245* Note that count is used in wait guard even though it is
244246* not protected by lock. This works because count can
245247* only decrease at this point (all other puts are shut
246248* out by lock), and we (or some other waiting put) are
247249* signalled if it ever changes from
248250* capacity. Similarly for all other uses of count in
249251* other wait guards.
255
305
361
398
257
303
361
398
propagate to a non-interrupted thread
270229
272
* Inserts the specified element at the tail of this queue, waiting if
271273* necessary up to the specified wait time for space to become available.
277275* @return <tt>true</tt> if successful, or <tt>false</tt> if
278276* the specified waiting time elapses before space is available.
522582* Atomically removes all of the elements from this queue.
523583* The queue will be empty after this call returns.
551621Transfer the elements outside of locks
590667* Returns an iterator over the elements in this queue in proper sequence.
591668* The returned <tt>Iterator</tt> is a "weakly consistent" iterator that
593670* and guarantees to traverse elements as they existed upon
594671* construction of the iterator, and may (but is not guaranteed to)
595672* reflect any modifications subsequent to construction.
605682* Basic weak-consistent iterator. At all times hold the next
606683* item to hand out so that if hasNext() reports true, we will
607684* still have it to return even if lost race with a take etc.
683762* Save the state to a stream (that is, serialize it).
685764* @serialData The capacity is emitted (int), followed by all of
686765* its elements (each an <tt>Object</tt>) in the proper order,
687766* followed by a null
688
712
767
791
* @param s the stream
695774Write out any hidden stuff, plus capacity
698777Write out all elements in the proper order.
702781Use trailing null as sentinel
710789* Reconstitute this queue instance from a stream (that is,
711790* deserialize it).
716795Read in capacity, and any hidden stuff
722801Read in all elements and place in queue

to top

Matching Instruction Sequences
File1 Line# File2 Line# Number of matching instructions
8 7 37
138 139 19
194 196 171
365 420 10
420 365 10
472 481 30
503 508 11
505 565 14
529 591 14
546 616 17
568 642 11
580 657 60
671 750 28

to top

Matching Identifiers
6903933977591709194L AbstractQueue add Array atomic AtomicInteger await awaitNanos
BlockingQueue capacity ClassNotFoundException clear Collection concurrent Condition count
current currentElement defaultReadObject defaultWriteObject drainTo equals extract first
fullyLock fullyUnlock get getAndAdd getAndDecrement getAndIncrement getAndSet getClass
getComponentType hasNext head ie IllegalArgumentException IllegalStateException insert InterruptedException
io IOException item Iterator Itr java lang last
lastRet length LinkedBlockingQueue lock lockInterruptibly locks MAX_VALUE maxElements
nanos newCondition newInstance next Node NoSuchElementException notEmpty notFull
NullPointerException Object ObjectInputStream ObjectOutputStream offer peek poll put
putLock readObject ReentrantLock reflect remainingCapacity remove removed Serializable
serialVersionUID set signal signalAll signalNotEmpty signalNotFull size take
takeLock timeout TimeUnit toArray toNanos toString trail unit
unlock util writeObject

to top

Partially Matching Identifiers
*** NONE ***
to the top
SCORE 80

CodeSuite copyright 2003-2012 by Software Analysis and Forensic Engineering Corporation