一、ReentrantLock的使用
在Java多线程中,可以使用synchronized关键字来实现线程之间同步互斥,但在JDK1.5中新增加了ReentrantLock类也能达到同样的效果,并且在扩展功能上也更加强大,比如具有嗅探锁定、多路分支通知等功能,而且在使用上也比synchronized更加的灵活。
1、使用ReentrantLock实现同步
既然ReentrantLock类在功能上相比synchronized更多,那么就以一个初步的程序示例
public class MyService {
private Lock lock = new ReentrantLock();
public void testMethod() {
lock.lock(); //获取锁,线程就持有了“对象监视器”
for (int i=0; i<3; i++) {
System.out.println("ThreadName=" + Thread.currentThread().getName()
+ (" " + (i+1)));
}
lock.unlock(); //释放锁
}
}
public class MyThread extends Thread {
private MyService myService;
public MyThread(MyService myService) {
this.myService = myService;
}
public void run() {
myService.testMethod();
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyService service = new MyService();
MyThread a1 = new MyThread(service);
MyThread a2 = new MyThread(service);
a1.start();
a2.start();
}
}
ThreadName=Thread-1 1
ThreadName=Thread-1 2
ThreadName=Thread-1 3
ThreadName=Thread-0 1
ThreadName=Thread-0 2
ThreadName=Thread-0 3
从运行的结果来看,当前线程打印完毕之后将锁进行释放,其他线程才可以继续打印。线程打印的数据是分组打印,因为当前线程已经持有锁,但线程之间打印的顺序是随机的。
2、使用Condition实现等待/通知:错误用法与解决
关键字synchronized与waits和notifyn/notifyAll()方法相结合可以实现等待/通知模式,类ReentrantLock也可以实现同样的功能,但需要借助于Condition对象。Condition类是在JDK5中出现的技术,使用它有更好的灵活性,比如可以实现多路通知功能,也就是在一个Lock对象里面可以创建多个Condition(即对象监视器)实例,线程对象可以注册在指定的Condition中,从而可以有选择性地进行线程通知,在调度线程上更加灵活。
在使用notify()/notifyAll()方法进行通知时,被通知的线程却是由JVM随机选择的。但使用ReentrantLock结合Condition类是可以实现前面介绍过的“选择性通知”,这个功能是非常重要的,而且在Condition类中是默认提供的。
而synchronized就相当于整个Lock对象中只有一个单一的Condition对象,所有的线程都注册在它一个对象的身上。线程开始notifyAll()时,需要通知所有的WAITING线程,没有选择权,会出现相当大的效率问题。
public class MyService {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void await() {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 自定义线程
public class MyThread1 extends Thread {
private MyService service;
public MyThread1(MyService service) {
this.service = service;
}
@Override
public void run() {
service.await();
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyService service = new MyService();
MyThread1 a1 = new MyThread1(service);
a1.start();
}
}
Exception in thread “Thread-0” java.lang.IllegalMonitorStateException
报错的异常信息是监视器出错,解决的办法是必须在condition.await()方法调用之前调用lock.lock()代码获得同步监视器。
正确使用Condition实现等待/通知
public class MyService {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void await() {
try {
lock.lock();
System.out.println("await 时间为: " + System.currentTimeMillis());
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void signal() {
try {
lock.lock();
System.out.println("signal 时间为:" + System.currentTimeMillis());
condition.signal();
} finally {
lock.unlock();
}
}
}
public class MyThread1 extends Thread {
private MyService service;
public MyThread1(MyService service) {
this.service = service;
}
@Override
public void run() {
service.await();
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyService service = new MyService();
MyThread1 a1 = new MyThread1(service);
a1.start();
Thread.sleep(1000);
service.signal();
}
}
await 时间为: 1462595312580
signal 时间为:1462595313580
Object类中的wait()方法相当于Condition类中的await()方法。
Object类中的wait(Iong timeout)方法相当于Condition类中的await(long time, TimeUnit unit)方法。
Object类中的notify()方法相当于Condition类中的signal()方法。
Object类中的notifyAll()方法相当于Condition类中的signalAll()方法。
3、使用Condition实现通知线程
3.1、通知全部线程
如果线程共用一个Condition,则signalAll()会唤醒所有的线程
public class MyService {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition(); //一个公共的
public void awaitA() {
try {
lock.lock();
System.out.println(" begin awaitA 时间为: " + System.currentTimeMillis()
+ " ThreadName=" + Thread.currentThread().getName());
condition.await();
System.out.println(" end awaitA 时间为: " + System.currentTimeMillis()
+ " ThreadName=" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void awaitB() {
try {
lock.lock();
System.out.println(" begin awaitA 时间为: " + System.currentTimeMillis()
+ " ThreadName=" + Thread.currentThread().getName());
condition.await();
System.out.println(" end awaitA 时间为: " + System.currentTimeMillis()
+ " ThreadName=" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void signalAll() {
try {
lock.lock();
System.out.println("signal 时间为:" + System.currentTimeMillis());
condition.signalAll();
} finally {
lock.unlock();
}
}
}
public class MyThread1 extends Thread {
private MyService service;
public MyThread1(MyService service) {
this.service = service;
}
@Override
public void run() {
service.awaitA();
}
}
public class MyThread2 extends Thread{
private MyService service;
public MyThread2(MyService service) {
this.service = service;
}
@Override
public void run() {
service.awaitB();
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyService service = new MyService();
MyThread1 a = new MyThread1(service);
a.setName("AA");
a.start();
MyThread2 b = new MyThread2(service);
b.setName("BB");
b.start();
Thread.sleep(2000);
service.signalAll();
}
}
begin awaitA 时间为: 1462628140485 ThreadName=AA
begin awaitA 时间为: 1462628140487 ThreadName=BB
signal 时间为:1462628142485
end awaitA 时间为: 1462628142485 ThreadName=AA
end awaitA 时间为: 1462628142485 ThreadName=BB
程序运行后,线程A和线程B都被唤醒了。
如果想单独唤醒部分线程该怎么处理呢?这时就有必要使用多个Condition对象了,也就是Condition对象可以唤醒部分指定线程,有助于提升程序运行的效率。可以先对线程进行分组,然后再唤醒指定组中的线程。
3.2、唤醒单个线程
public class MyService {
private Lock lock = new ReentrantLock();
//使用多个Condition可以单独唤醒部分线程!!!
private Condition conditionA = lock.newCondition();
private Condition conditionB = lock.newCondition();
public void awaitA() {
try {
lock.lock();
System.out.println(" begin awaitA 时间为: " + System.currentTimeMillis()
+ " ThreadName=" + Thread.currentThread().getName());
conditionA.await();
System.out.println(" end awaitA 时间为: " + System.currentTimeMillis()
+ " ThreadName=" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void awaitB() {
try {
lock.lock();
System.out.println(" begin awaitA 时间为: " + System.currentTimeMillis()
+ " ThreadName=" + Thread.currentThread().getName());
conditionB.await();
System.out.println(" end awaitA 时间为: " + System.currentTimeMillis()
+ " ThreadName=" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void signalAll_A() {
try {
lock.lock();
System.out.println("signal 时间为:" + System.currentTimeMillis());
conditionA.signalAll();
} finally {
lock.unlock();
}
}
public void signalAll_B() {
try {
lock.lock();
System.out.println("signal 时间为:" + System.currentTimeMillis());
conditionB.signalAll();
} finally {
lock.unlock();
}
}
}
// 两个自定义的线程同上
public class Run {
public static void main(String[] args) throws InterruptedException {
MyService service = new MyService();
MyThread1 a = new MyThread1(service);
a.setName("AA");
a.start();
MyThread2 b = new MyThread2(service);
b.setName("BB");
b.start();
Thread.sleep(2000);
service.signalAll_A(); //这里只唤醒线程A
}
}
begin awaitA 时间为: 1462629180458 ThreadName=BB
begin awaitA 时间为: 1462629180469 ThreadName=AA
signal 时间为:1462629182457
end awaitA 时间为: 1462629182457 ThreadName=AA
程序运行后,只有线程A被唤醒了,线程B没有唤醒
通过此实验可以得知,使用ReentrantLock对象可以唤醒指定种类的线程,这是控制部分线程行为的方便方式。
二、公平锁和非公平锁
公平与非公平锁:锁Lock分为“公平锁”和“非公平锁”,公平锁表示线程获取锁的顺序是按照线程加锁的顺序来分配的,即先来先得的FIFO先进先出顺序。而非公平锁就是一种获取锁的抢占机制,是随机获得锁的,和公平锁不一样的就是先来的不一定先得到锁,这个方式可能造成某些线程一直拿不到锁,结果也就是不公平的了。
1、公平锁实例
public class Service {
private ReentrantLock lock ;
public Service(boolean isFair) {
lock = new ReentrantLock(isFair);
}
public void serviceMethod() {
try {
lock.lock();
System.out.println("ThreadName=" + Thread.currentThread().getName()
+ " 获得锁定");
} finally {
lock.unlock();
}
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
final Service service = new Service(true); //改为false就为非公平锁了
Runnable runnable = new Runnable() {
public void run() {
System.out.println("**线程: " + Thread.currentThread().getName()
+ " 运行了 " );
service.serviceMethod();
}
};
Thread[] threadArray = new Thread[10];
for (int i=0; i<10; i++) {
threadArray[i] = new Thread(runnable);
}
for (int i=0; i<10; i++) {
threadArray[i].start();
}
}
}
**线程: Thread-0 运行了
ThreadName=Thread-0 获得锁定
**线程: Thread-1 运行了
ThreadName=Thread-1 获得锁定
**线程: Thread-4 运行了
**线程: Thread-5 运行了
ThreadName=Thread-4 获得锁定
**线程: Thread-3 运行了
**线程: Thread-7 运行了
**线程: Thread-9 运行了
ThreadName=Thread-5 获得锁定
ThreadName=Thread-3 获得锁定
**线程: Thread-2 运行了
ThreadName=Thread-7 获得锁定
ThreadName=Thread-9 获得锁定
ThreadName=Thread-2 获得锁定
**线程: Thread-6 运行了
ThreadName=Thread-6 获得锁定
**线程: Thread-8 运行了
ThreadName=Thread-8 获得锁定
打印的结果是按照线程加锁的顺序输出的,即线程运行了,则会先获得锁
把Run类里的true改为false就为非公平锁了
**线程: Thread-1 运行了
**线程: Thread-4 运行了
ThreadName=Thread-1 获得锁定
**线程: Thread-3 运行了
ThreadName=Thread-4 获得锁定
**线程: Thread-6 运行了
**线程: Thread-5 运行了
**线程: Thread-2 运行了
**线程: Thread-0 运行了
ThreadName=Thread-6 获得锁定
**线程: Thread-7 运行了
ThreadName=Thread-7 获得锁定
**线程: Thread-8 运行了
ThreadName=Thread-8 获得锁定
ThreadName=Thread-3 获得锁定
**线程: Thread-9 运行了
ThreadName=Thread-9 获得锁定
ThreadName=Thread-5 获得锁定
ThreadName=Thread-2 获得锁定
ThreadName=Thread-0 获得锁定
是乱序的,说明先start()启动的线程不代表先获得锁
三、读写锁
类ReentrantLock具有完全互斥排他的效果,即同一时间只有一个线程在执行ReentrantLock.lock()方法后面的任务。这样做虽然保证了实例变量的线程安全性,但效率却是非常低下的。所以在JDK中提供了一种读写锁ReentrantReadWriteLock类,使用它可以加快运行效率,在某些不需要操作实例变量的方法中,完全可以使用读写锁ReentrantReadWriteLock来提升该方法的代码运行速度。
读写锁表示也有两个锁,一个是读操作相关的锁,也称为共享锁;另一个是写操作相关的锁,也叫排他锁。也就是多个读锁之间不互斥,读锁与写锁互斥,写锁与写锁互斥。在没有线程Thread进行写入操作时,进行读取操作的多个Thread都可以获取读锁,而进行写入操作的Thread只有在获取写锁后才能进行写入操作。即多个Thread可以同时进行读取操作,但是同一时刻只允许一个Thread进行写入操作。
1、读读共享
public class Service {
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void read() {
try {
lock.readLock().lock();
System.out.println("获得读锁:" +Thread.currentThread().getName()
+" " +System.currentTimeMillis());
} finally {
lock.readLock().unlock();
}
}
}
// 两个自定义线程
public class MyThread1 extends Thread {
private Service service;
public MyThread1(Service service) {
this.service = service;
}
@Override
public void run() {
service.read();
}
}
public class MyThread2 extends Thread{
private Service service;
public MyThread2(Service service) {
this.service = service;
}
@Override
public void run() {
service.read();
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
Service service = new Service();
MyThread1 a = new MyThread1(service);
a.setName("AA");
a.start();
MyThread2 b = new MyThread2(service);
b.setName("BB");
b.start();
}
}
获得读锁:AA 1462676138838
获得读锁:BB 1462676138841
从控制台中打印的时间来看,两个线程几乎同时进人lock方法后面的代码。说明在此使用了lock.readLock()读锁可以提高程序运行效率,.允许多个线程同时执行locks方法后面的代码。
2、写写互斥
public class Service {
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void write() {
try {
lock.writeLock().lock();
System.out.println("获得写锁:" +Thread.currentThread().getName()
+" " +System.currentTimeMillis());
} finally {
lock.writeLock().unlock();
}
}
}
上面自定义的线程将read方法改为为write方法。Run类不变,结果如下
获得写锁:BB 1462676627323
获得写锁:AA 1462676627323
使用写锁lock.writeLock()的效果就是同一时间只允许一个线程执行lock方法后面的代码
3、读写/写读互斥
public class Service {
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void read() {
try {
lock.readLock().lock();
System.out.println("获得读锁:" +Thread.currentThread().getName()
+" " +System.currentTimeMillis());
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.readLock().unlock();
}
}
public void write() {
try {
lock.writeLock().lock();
System.out.println("获得写锁:" +Thread.currentThread().getName()
+" " +System.currentTimeMillis());
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.writeLock().unlock();
}
}
}
自定义的线程MyThread1里的run为read方法,MyThread2里的run方法为write.
获得写锁:BB 1462677688372
获得读锁:AA 1462677691372
说明‘“读写”是互斥的
“读写”“、”写读“、”写写“都是互斥的;而”读读“是异步的,非互斥的