提交 9abf5e1d authored 作者: Thomas Mueller's avatar Thomas Mueller

A concurrent list, to replace the array list of old roots.

上级 1f9fcba2
...@@ -17,122 +17,103 @@ import java.util.Iterator; ...@@ -17,122 +17,103 @@ import java.util.Iterator;
*/ */
public class ConcurrentArrayList<K> { public class ConcurrentArrayList<K> {
// /** /**
// * The list. * The array.
// */ */
// volatile List<K> list = new List<K>(null, 0, 0); @SuppressWarnings("unchecked")
// K[] array = (K[]) new Object[0];
// /**
// * Get the first element, or null if none. /**
// * * Get the first element, or null if none.
// * @return the first element *
// */ * @return the first element
// public K peekFirst() { */
// return list.peekFirst(); public K peekFirst() {
// } K[] a = array;
// return a.length == 0 ? null : a[0];
// /** }
// * Get the last element, or null if none.
// * /**
// * @return the last element * Get the last element, or null if none.
// */ *
// public K peekLast() { * @return the last element
// return list.peekLast(); */
// } public K peekLast() {
// K[] a = array;
// /** int len = a.length;
// * Add an element at the end. return len == 0 ? null : a[len - 1];
// * }
// * @param obj the element
// */ /**
// public synchronized void add(K obj) { * Add an element at the end.
// K[] array = new K[list.length + 1]; *
// if (list.length > 0) { * @param obj the element
// System.arraycopy(list.list, list.offset, dest, destPos, length) */
// Arrays.copyOf(, newLength) public synchronized void add(K obj) {
// list = new List<K>(list.add(obj); int len = array.length;
// } array = Arrays.copyOf(array, len + 1);
// array[len] = obj;
// /** }
// * Remove the first element, if it matches.
// * /**
// * @param obj the element to remove * Remove the first element, if it matches.
// * @return true if the element matched and was removed *
// */ * @param obj the element to remove
// public synchronized boolean removeFirst(K obj) { * @return true if the element matched and was removed
// if (head.obj != obj) { */
// return false; public synchronized boolean removeFirst(K obj) {
// } if (peekFirst() != obj) {
// head = head.next; return false;
// return true; }
// } int len = array.length;
// @SuppressWarnings("unchecked")
// /** K[] a = (K[]) new Object[len - 1];
// * Remove the last element, if it matches. System.arraycopy(array, 1, a, 0, len - 1);
// * array = a;
// * @param obj the element to remove return true;
// * @return true if the element matched and was removed }
// */
// public synchronized boolean removeLast(K obj) { /**
// if (peekLast() != obj) { * Remove the last element, if it matches.
// return false; *
// } * @param obj the element to remove
// head = Entry.removeLast(head); * @return true if the element matched and was removed
// return true; */
// } public synchronized boolean removeLast(K obj) {
// if (peekLast() != obj) {
// /** return false;
// * Get an iterator over all entries. }
// * array = Arrays.copyOf(array, array.length - 1);
// * @return the iterator return true;
// */ }
// public Iterator<K> iterator() {
// return new Iterator<K>() { /**
// * Get an iterator over all entries.
// List<K> list = head; *
// * @return the iterator
// @Override */
// public boolean hasNext() { public Iterator<K> iterator() {
// return current != NULL; return new Iterator<K>() {
// }
// K[] a = array;
// @Override int index;
// public K next() {
// K x = current.obj; @Override
// current = current.next; public boolean hasNext() {
// return x; return index < a.length;
// } }
//
// @Override @Override
// public void remove() { public K next() {
// throw DataUtils.newUnsupportedOperationException("remove"); return a[index++];
// } }
//
// }; @Override
// } public void remove() {
// throw DataUtils.newUnsupportedOperationException("remove");
// /** }
// * An entry in the linked list.
// */ };
// private static class List<K> { }
// final K[] list;
// final int offset;
// final int length;
//
// List(K[] list, int offset, int length) {
// this.list = list;
// this.offset = offset;
// this.length = length;
// }
//
// public K peekFirst() {
// return length == 0 ? null : list[offset];
// }
//
// public K peekLast() {
// return length == 0 ? null : list[offset + length - 1];
// }
//
// }
} }
...@@ -53,8 +53,8 @@ public class MVMap<K, V> extends AbstractMap<K, V> ...@@ -53,8 +53,8 @@ public class MVMap<K, V> extends AbstractMap<K, V>
private final DataType keyType; private final DataType keyType;
private final DataType valueType; private final DataType valueType;
private ConcurrentLinkedList<Page> oldRoots = private ConcurrentArrayList<Page> oldRoots =
new ConcurrentLinkedList<Page>(); new ConcurrentArrayList<Page>();
private boolean closed; private boolean closed;
private boolean readOnly; private boolean readOnly;
......
...@@ -10,7 +10,7 @@ import java.util.LinkedList; ...@@ -10,7 +10,7 @@ import java.util.LinkedList;
import java.util.Random; import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.h2.mvstore.ConcurrentLinkedList; import org.h2.mvstore.ConcurrentArrayList;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.Task; import org.h2.util.Task;
...@@ -48,8 +48,10 @@ public class TestConcurrentLinkedList extends TestBase { ...@@ -48,8 +48,10 @@ public class TestConcurrentLinkedList extends TestBase {
private void testPerformance(final boolean stock) { private void testPerformance(final boolean stock) {
System.out.print(stock ? "stock " : "custom "); System.out.print(stock ? "stock " : "custom ");
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
final ConcurrentLinkedList<Integer> test = new ConcurrentLinkedList<Integer>(); // final ConcurrentLinkedList<Integer> test =
// final ConcurrentRing<Integer> test = new ConcurrentRing<Integer>(); // new ConcurrentLinkedList<Integer>();
final ConcurrentArrayList<Integer> test =
new ConcurrentArrayList<Integer>();
final LinkedList<Integer> x = new LinkedList<Integer>(); final LinkedList<Integer> x = new LinkedList<Integer>();
final AtomicInteger counter = new AtomicInteger(); final AtomicInteger counter = new AtomicInteger();
Task task = new Task() { Task task = new Task() {
...@@ -107,7 +109,7 @@ public class TestConcurrentLinkedList extends TestBase { ...@@ -107,7 +109,7 @@ public class TestConcurrentLinkedList extends TestBase {
} }
private void testConcurrent() { private void testConcurrent() {
final ConcurrentLinkedList<Integer> test = new ConcurrentLinkedList<Integer>(); final ConcurrentArrayList<Integer> test = new ConcurrentArrayList<Integer>();
// final ConcurrentRing<Integer> test = new ConcurrentRing<Integer>(); // final ConcurrentRing<Integer> test = new ConcurrentRing<Integer>();
final AtomicInteger counter = new AtomicInteger(); final AtomicInteger counter = new AtomicInteger();
final AtomicInteger size = new AtomicInteger(); final AtomicInteger size = new AtomicInteger();
...@@ -140,7 +142,7 @@ public class TestConcurrentLinkedList extends TestBase { ...@@ -140,7 +142,7 @@ public class TestConcurrentLinkedList extends TestBase {
private void testRandomized() { private void testRandomized() {
Random r = new Random(0); Random r = new Random(0);
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
ConcurrentLinkedList<Integer> test = new ConcurrentLinkedList<Integer>(); ConcurrentArrayList<Integer> test = new ConcurrentArrayList<Integer>();
// ConcurrentRing<Integer> test = new ConcurrentRing<Integer>(); // ConcurrentRing<Integer> test = new ConcurrentRing<Integer>();
LinkedList<Integer> x = new LinkedList<Integer>(); LinkedList<Integer> x = new LinkedList<Integer>();
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
......
...@@ -3,10 +3,12 @@ ...@@ -3,10 +3,12 @@
* and the EPL 1.0 (http://h2database.com/html/license.html). * and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group * Initial Developer: H2 Group
*/ */
package org.h2.mvstore; package org.h2.dev.util;
import java.util.Iterator; import java.util.Iterator;
import org.h2.mvstore.DataUtils;
/** /**
* A very simple linked list that supports concurrent access. * A very simple linked list that supports concurrent access.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论