Lock
Lock是Java提供给开发者JDK级别的一种控制代码同步访问的工具类。只有一个子类ReentrantLock。功能和synchronized差不多。使用也是非常的简单
class Test {
private Lock lock = new ReentrantLock();
//需要同步访问的方法
private void doSomething() {
lock.lock();
try {
//do something
} catch (Exception e) {
}finally {
lock.unlock();
}
}
}
例子
private void lockTest() {
sendMessage("lockTest begin");
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
doSomething();
}
sendMessage("thread-1 end");
}
}, "thread-1").start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
doSomething();
}
sendMessage("thread-2 end");
}
}, "thread-2").start();
}
//一次访问这个函数
private void doSomething() {
lock.lock();
try {
Thread.sleep(100);
sendMessage(Thread.currentThread().getName() + " doSomething");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
输出:
ReadWriteLock
同步读写操作的一个锁。和Lock完全没有任何关系。
- 允许多个线程同时进行读操作。
- 不允许多个线程同时写操作,只会谁拿到锁谁先去执行写操作。
- 多个线程既有读又有写操作,没有线程进入写或者等待写,就可以多个线程读了;进入写操作后,不允许其他线程读或者写
读操作
private void readLockTest() {
new Thread(new Runnable() {
@Override
public void run() {
readSomething();
}
}, "t1").start();
new Thread(new Runnable() {
@Override
public void run() {
readSomething();
}
}, "t2").start();
}
private void readSomething() {
readWriteLock.readLock().lock();
try {
Iterator<String> iterator = cacheList.iterator();
while (iterator.hasNext()) {
Thread.sleep(100);
sendMessage(Thread.currentThread().getName() + "读取 -- "+ iterator.next());
}
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
readWriteLock.readLock().unlock();
}
}
输出:
写操作
private void writeLockTest() {
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
writeSomething();
}
}, "t1");
t1.start();
final Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
writeSomething();
}
}, "t2");
t2.start();
new Thread(new Runnable() {
@Override
public void run() {
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
sendMessage("写完了");
for (String s : cacheList) {
sendMessage(s);
}
}
}).start();
}
private void writeSomething() {
readWriteLock.writeLock().lock();
try{
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
cacheList.add("这是第" + i + "元素");
sendMessage(Thread.currentThread().getName() + "写入---这是第" + i + "元素");
}
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
readWriteLock.writeLock().unlock();
}
}
输出:
读写同时进行
private void readWriteLockTest(){
new Thread(new Runnable() {
@Override
public void run() {
writeSomething();
}
}, "t1").start();
new Thread(new Runnable() {
@Override
public void run() {
readSomething();
}
}, "t2").start();
new Thread(new Runnable() {
@Override
public void run() {
readSomething();
}
}, "t3").start();
new Thread(new Runnable() {
@Override
public void run() {
writeSomething();
}
}, "t4").start();
输出:
测试方法中有四个线程,分别是:写,读,读,写;日志中在t2读的时候,这是t3启动了,t3是读的操作,此时并没有写或者等待写的操作,所以t3也开始读,最终t2和t3同时读。
可以看出在t2的时候,t3启动了,因为t3是写操作,所以t4启动后,因为t3已经等待写,所以t4会在t3写完后才执行。