5. == equals
char[] value
str1 str1 int length
boolean equals(Object obj)
char[] value
str2 str2 int length
boolean equals(Object obj)
str1 == str2 str1 str2
char[] value
str1.equals(str2)
6. java.lang.String
public final class String implements java.io.Serializable, Comparable<String>, CharSequence{
private final char value[];
private final int count;
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
34. Java.util.concurrent
import java.util.concurrent.atomic.*;
public class Account{
private AtomicInteger amount;
Account(int amount){
this.amount = new AtomicInteger(amount);
}
public int get(int amount){
if(this.amount.get() < amount){
return 0;
}
do{
if(this.amount.get() < amount){
break;
}
} while(!this.amount.compareAndSet(this.amount.get(), this.amount.get() - amount));
return amount;
}
public void put(int amount){
do{
if(amount < this.amount.get()){
break;
}
} while(!this.amount.compareAndSet(this.amount.get(), this.amount.get() + amount));
}
public int getAmount(){
return amount.get();
}
}
35. AtomicInteger
public class AtomicInteger extends Number implements java.io.Serializable {
private volatile int value;
public AtomicInteger(int initialValue) {
value = initialValue;
}
public final int get() {
return value;
}
public final void set(int newValue) {
value = newValue;
}
public final int getAndSet(int newValue) {
for (;;) {
int current = get();
if (compareAndSet(current, newValue))
return current;
}
}
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
}
ïź CAS
ïź CAS
ïź Int CAS
36. CAS Compare And Swap
1 2
100
0
0
100
CAS
ïź
ïź
ïź 2 0 CAS
100