1
package org.apache.maven.surefire.util.internal;
4
* Licensed to the Apache Software Foundation (ASF) under one
5
* or more contributor license agreements. See the NOTICE file
6
* distributed with this work for additional information
7
* regarding copyright ownership. The ASF licenses this file
8
* to you under the Apache License, Version 2.0 (the
9
* "License"); you may not use this file except in compliance
10
* with the License. You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing,
15
* software distributed under the License is distributed on an
16
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
* KIND, either express or implied. See the License for the
18
* specific language governing permissions and limitations
23
* A producer/consumer queue that is optimized for *one* producer thread
24
* and *one* consumer thread, and solely optimized for efficient inserts
25
* by the producer, minimizing producer locking for hand-off to
28
* The producer can actually come in on different threads
29
* (because lastInserted is volatile), but can/will lose
30
* items if they arrive concurrently. Take only supports a single
33
* This runs like crazy, but is not the most garbage friendly around.
35
* TwoThreadBlockingQueue insert 5000000 elements in = 52ms
36
* LinkedBlockingQueue insert 5000000 elements in = 179ms
37
* LikedBlockingDeque insert 5000000 elements in = 114ms
38
* ArrayList insert 5000000 elements in = 18ms (sized at correct size from start)
40
* @author Kristian Rosenvold
42
public class TwoThreadBlockingQueue
43
implements BlockingQueue
45
private volatile Element lastInserted;
46
private volatile Element lastTaken;
47
private volatile Element first;
49
public static final Object poison = new Object();
51
public void add( Object object )
53
Element next = new Element( object);
54
if (lastInserted == null){
55
first = lastInserted = next;
57
lastInserted.next = next;
63
throws InterruptedException
65
if (lastTaken == null){
66
while (first == null){
72
Element next = lastTaken.next;
75
next = lastTaken.next;
79
return lastTaken.object;
82
private static class Element
84
private final Object object;
86
private volatile Element next;
88
Element( Object object )