锁
synchronized是最基本的同步机制,Java5引入了另外一种更强大更灵活的同步机制:Lock接口及其实现类。Lock接口的引入使得同步机制不再局限于关键字,而可以在API一层实现,深远的影响了Java的并发基础设施。
相比synchronized,Lock机制有以下优点:
- 支持更灵活的同步代码块结构。使用synchronized关键字时,只能在同一个synchronized块结构中获取和释放控制。Lock接口允许实现更复杂的临界区结构,临界区的控制和释放不出现在同一个块结构中。
- 相比synchronized关键字,Lock接口提供了更多的功能。
- Lock接口允许分离读和写操作,允许多个读线程和一个写线程。
- 相比synchronized关键字,Lock接口具有更好的性能。
Lock的获取和释放都需要开发人员控制,所以要特别注意锁的释放!下面以一个计数器的实现为例说明Lock接口的使用方法:
public class CountLockDemo {
public static void main(String[] args){
Lock lock = new ReentrantLock();
Counter counter = new Counter(lock);
System.out.println("main:创建10个线程");
Thread[] threads = new Thread[10];
for(int i=0; i<threads.length ;i++){
threads[i] = new Thread(counter);
}
System.out.println("main:启动10个线程");
for(int i=0; i<threads.length ;i++){
threads[i].start();
}
System.out.println("main:等待10个线程");
try{
for(int i=0; i<threads.length ;i++){
threads[i].join();
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println("main:退出");
}
}
class Counter implements Runnable{
private long count = 0;
private Lock lock = null;
public Counter(Lock lock){
this.lock = lock;
}
@Override
public void run() {
lock.lock();
try{
count += 1;
long duration = (long)(Math.random()*1000);
Thread.sleep(duration);
System.out.println(Thread.currentThread().getName() + ":" + count);
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
lock.unlock();
}
}
}
程序运行日志:
main:创建10个线程
main:启动10个线程
main:等待10个线程
Thread-0:1
Thread-2:2
Thread-4:3
Thread-6:4
Thread-8:5
Thread-1:6
Thread-7:7
Thread-9:8
Thread-5:9
Thread-3:10
main:退出