A).一般认为synchronized关键字的实现是源自于像信号量之类的线程同步机制,涉及到线程运行状态的切换,在高并发状态下,CPU消耗过多的时间在线程的调度上,从而造成了性能的极大浪费。然而真的如此么?
线程的状态主要有一下五种,分别是新建状态,就绪状态,运行状态,阻塞状态,消亡状态等5种状态
B).lock实现原理则是依赖于硬件,现代处理器都支持CAS指令,所谓CAS指令简单的来说Compare And Set,CPU循环执行指令直到得到所期望的结果,换句话来说就是当变量真实值不等于当前线程调用时的值的时候(说明其他线程已经将这个值改变),就不会赋予变量新的值。这样就保证了变量在多线程环境下的安全性。
然而,现实情况是当JDK版本高于1.6的时候,synchronized已经被做了CAS的优化:具体是这样的,当执行到synchronized代码块时,先对对象头的锁标志位用lock cmpxchg的方式设置成“锁住“状态,释放锁时,在用lock cmpxchg的方式修改对象头的锁标志位为”释放“状态,写操作都立刻写回主内存。JVM会进一步对synchronized时CAS失败的那些线程进行阻塞操作(调用操作系统的信号量)(此段来摘自别处)。也就是先CAS操作,不行的话继而阻塞线程。
除此之外,系统环境,CPU架构,虚拟机环境都会影响两者的性能关系。
举例如下
1).X86_64 cpu i7 4910mq @4.0ghz ,Windows10 64bit,JDK1.8 hotspot 64bit虚拟机环境
测试代码
测试对某Map对象高并发下的读写线程安全测试
测试对比有synchronized,ReadWriteLock,ConcurrentHashMap,
public class MapTest {
private Map map = new ConcurrentHashMap<>();
private long starttime;
private AtomicInteger count = new AtomicInteger(t_count);
private final static int t_count = 5000;
private final static int rw_count = 10000;
Runnable readrun = new Runnable() {
@Override
public void run() {
int i = rw_count;
while (i > 0){
map.get(i);
i--;
}
System.out.println("read-mapsize="+map.size());
if(count.decrementAndGet() == 0)
System.out.println("time="+ (System.currentTimeMillis() - starttime +"ms"));
}
};
Runnable writerun = new Runnable() {
@Override
public void run() {
int i = rw_count;
while (i > 0){
map.put(i,i+"");
i--;
}
System.out.println("write-mapsize="+map.size());
if(count.decrementAndGet() == 0)
System.out.println("time="+ (System.currentTimeMillis() - starttime + "ms"));
}
};
public void run(){
starttime = System.currentTimeMillis();
for(int i = 0;i < t_count/2;i ++){
new Thread(writerun).start();
new Thread(readrun).start();
}
}
}
HashMap 用synchronized重写
public class SyncHashMap extends HashMap{
@Override
public Object get(Object key) {
// TODO Auto-generated method stub
synchronized (this) {
return super.get(key);
}
}
@Override
public synchronized Object put(Object key, Object value) {
// TODO Auto-generated method stub
synchronized (this) {
return super.put(key, value);
}
}
}
用读写锁实现的Map代理类,有些粗糙,没加try finally
public class SyncMapProxy implements Map{
private Map origin;
private ReadWriteLock lock;
public SyncMapProxy(Map origin) {
this.origin = origin;
lock = new ReentrantReadWriteLock();
}
public static SyncMapProxy SyncMap(Map map){
return new SyncMapProxy(map);
}
@Override
public void clear() {
lock.writeLock().lock();
origin.clear();
lock.writeLock().unlock();
}
@Override
public boolean containsKey(Object key) {
lock.readLock().lock();
boolean res = origin.containsKey(key);
lock.readLock().unlock();
return res;
}
@Override
public boolean containsValue(Object value) {
lock.readLock().lock();
boolean res = origin.containsKey(value);
lock.readLock().unlock();
return res;
}
@Override
public Set> entrySet() {
lock.readLock().lock();
Set> res = origin.entrySet();
lock.readLock().unlock();
return res;
}
@Override
public V get(Object key) {
lock.readLock().lock();
V res = origin.get(key);
lock.readLock().unlock();
return res;
}
@Override
public boolean isEmpty() {
return origin.isEmpty();
}
@Override
public Set keySet() {
lock.readLock().lock();
Set res = origin.keySet();
lock.readLock().unlock();
return res;
}
@Override
public V put(K key, V value) {
lock.writeLock().lock();
V v = origin.put(key, value);
lock.writeLock().unlock();
return v;
}
@Override
public void putAll(Map extends K, ? extends V> map) {
lock.writeLock().lock();
origin.putAll(map);
lock.writeLock().unlock();
}
@Override
public V remove(Object key) {
lock.writeLock().lock();
V v = origin.remove(key);
lock.writeLock().unlock();
return v;
}
@Override
public int size() {
return origin.size();
}
@Override
public Collection values() {
lock.readLock().lock();
Collection res = origin.values();
lock.readLock().unlock();
return res;
}
}