From 30101c9fe0e6bef48c3480e30ee7ed3dc4f625a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=AF=E7=BE=BD?= Date: Thu, 18 Feb 2016 19:09:32 +0800 Subject: [PATCH] add concurrent hash set --- .../darks/grid/network/discovery/TCPPING.java | 10 +- .../darks/grid/utils/ConcurrentHashSet.java | 124 ++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/main/java/darks/grid/utils/ConcurrentHashSet.java diff --git a/src/main/java/darks/grid/network/discovery/TCPPING.java b/src/main/java/darks/grid/network/discovery/TCPPING.java index 7220ce0..2a0ff0b 100644 --- a/src/main/java/darks/grid/network/discovery/TCPPING.java +++ b/src/main/java/darks/grid/network/discovery/TCPPING.java @@ -24,6 +24,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import darks.grid.GridException; import darks.grid.GridRuntime; import darks.grid.manager.GridNodesManager; import darks.grid.utils.ParamsUtils; @@ -92,7 +93,14 @@ public void setHosts(String hosts) this.hosts = hosts; if (hosts != null) { - tryAddressList = ParamsUtils.parseAddress(hosts); + try + { + tryAddressList = ParamsUtils.parseAddress(hosts); + } + catch (Exception e) + { + throw new GridException("Invalid TCPPING hosts:" + hosts, e); + } } } diff --git a/src/main/java/darks/grid/utils/ConcurrentHashSet.java b/src/main/java/darks/grid/utils/ConcurrentHashSet.java new file mode 100644 index 0000000..74f1533 --- /dev/null +++ b/src/main/java/darks/grid/utils/ConcurrentHashSet.java @@ -0,0 +1,124 @@ +/** + * + * Copyright 2015 The Darks Grid Project (Liu lihua) + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package darks.grid.utils; + +import java.io.Serializable; +import java.util.AbstractSet; +import java.util.Collection; +import java.util.Iterator; +import java.util.concurrent.ConcurrentHashMap; + +public class ConcurrentHashSet extends AbstractSet implements Serializable { + + /** + * + */ + private static final long serialVersionUID = -4868544861254524679L; + + private static final Object PRESENT = new Object(); + + private transient ConcurrentHashMap data = null; + + public ConcurrentHashSet() { + data = new ConcurrentHashMap(); + } + + public ConcurrentHashSet(int initialCapacity) { + data = new ConcurrentHashMap(initialCapacity); + } + + public ConcurrentHashSet(Collection c) { + data = new ConcurrentHashMap(c.size()); + addAll(c); + } + + + @Override + public Iterator iterator() { + return data.keySet().iterator(); + } + + @Override + public int size() { + return data.size(); + } + + @Override + public boolean equals(Object o) { + if (o != null && o instanceof ConcurrentHashSet) { + ConcurrentHashSet os = (ConcurrentHashSet) o; + return data.equals(os.data); + } + return false; + } + + @Override + public int hashCode() { + return data.hashCode(); + } + + @Override + public boolean isEmpty() { + return data.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return data.containsKey(o); + } + + @Override + public boolean add(E e) { + return data.put(e, PRESENT) == null; + } + + @Override + public boolean remove(Object o) { + return data.remove(o) != null; + } + + @Override + public void clear() { + data.clear(); + } + + private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { + // Write out any hidden serialization magic + s.defaultWriteObject(); + + // Write out size + s.writeInt(data.size()); + + // Write out all elements in the proper order. + for (E e : data.keySet()) + s.writeObject(e); + } + + private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, + ClassNotFoundException { + // Read in any hidden serialization magic + s.defaultReadObject(); + + int size = s.readInt(); + data = new ConcurrentHashMap(size); + + // Read in all elements in the proper order. + for (int i = 0; i < size; i++) { + E e = (E) s.readObject(); + data.put(e, PRESENT); + } + } +}