同步

一、线程同步

public synchronized void getM1(){ a(); } public synchronized void getM2(){ b(); c(); d(); }

 

二、加RW lock

private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private ReadLock readLock = lock.readLock(); private WriteLock writeLock = lock.writeLock(); public void a() { writeLock.lock(); try { // } finally { writeLock.unlock(); } } public void b() { readLock.lock(); try { // } finally { readLock.unlock(); } } public void c() { readLock.lock(); try { // } finally { readLock.unlock(); } } public void d() { readLock.lock(); try { // } finally { readLock.unlock(); } }

你可能感兴趣的:(c)