版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
Java 1.7
Yanzhen
Java 1.8
public class MyKey implements Comparable<MyKey>{
public int key;
@Override
public int compareTo(MyKey o) {
// TODO Auto-generated method stub
System.out.println("-----------------------------------------" );
System.out.println("cur key = "+ key + " o.key = "+ o.key );
return key - o.key;
}
public MyKey(int key) {
super();
this.key = key;
}
@Override
public int hashCode() {
// final int prime = 31;
// int result = 1;
// result = prime * result + key;
//return result;
return 100;
}
@Override
public String toString() {
return "MyKey [key=" + key + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyKey other = (MyKey) obj;
if (key != other.key)
return false;
return true;
}
}
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
public class Demo2KeyCompare {
final static Map<MyKey, Integer> count = new ConcurrentHashMap<>();
public static void main(String[] args) {
demo2();
}
public static void demo2() {
final CountDownLatch endLatch = new CountDownLatch(1);
Runnable task = new Runnable() {
@Override
public void run() {
Integer oldValue, newValue;
for (int i = 0; i < 10; i++) {
MyKey key = new MyKey(i);
while (true) {
oldValue = count.get(key);
if (null == oldValue) {
newValue = 1;
if (count.putIfAbsent(key, newValue) == null) {
break;
}
} else {
newValue = oldValue + 1;
if (count.replace(key, oldValue, newValue)) {
break;
}
}
}
System.out.println(Thread.currentThread().getName() + " current key:" + key + " newValue:" + newValue);
}
endLatch.countDown();
}
};
//new Thread(task).start();
new Thread(task).start();
try {
endLatch.await();
//System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Thread-0 current key:MyKey [key=0] newValue:1
Thread-0 current key:MyKey [key=1] newValue:1
Thread-0 current key:MyKey [key=2] newValue:1
Thread-0 current key:MyKey [key=3] newValue:1
Thread-0 current key:MyKey [key=4] newValue:1
Thread-0 current key:MyKey [key=5] newValue:1
Thread-0 current key:MyKey [key=6] newValue:1
Thread-0 current key:MyKey [key=7] newValue:1
Thread-0 current key:MyKey [key=8] newValue:1
-----------------------------------------
cur key = 1 o.key = 0
-----------------------------------------
cur key = 2 o.key = 0
-----------------------------------------
cur key = 2 o.key = 1
-----------------------------------------
cur key = 3 o.key = 1
-----------------------------------------
cur key = 3 o.key = 2
-----------------------------------------
cur key = 4 o.key = 1
-----------------------------------------
cur key = 4 o.key = 2
-----------------------------------------
cur key = 4 o.key = 3
-----------------------------------------
cur key = 5 o.key = 1
-----------------------------------------
cur key = 5 o.key = 3
-----------------------------------------
cur key = 5 o.key = 4
-----------------------------------------
cur key = 6 o.key = 1
-----------------------------------------
cur key = 6 o.key = 3
-----------------------------------------
cur key = 6 o.key = 4
-----------------------------------------
cur key = 6 o.key = 5
-----------------------------------------
cur key = 7 o.key = 1
-----------------------------------------
cur key = 7 o.key = 3
-----------------------------------------
cur key = 7 o.key = 5
-----------------------------------------
cur key = 7 o.key = 6
-----------------------------------------
cur key = 8 o.key = 3
-----------------------------------------
cur key = 8 o.key = 5
-----------------------------------------
cur key = 8 o.key = 6
-----------------------------------------
cur key = 8 o.key = 7
-----------------------------------------
cur key = 9 o.key = 3
-----------------------------------------
cur key = 9 o.key = 5
-----------------------------------------
cur key = 9 o.key = 7
-----------------------------------------
cur key = 9 o.key = 8
Thread-0 current key:MyKey [key=9] newValue:1