-
在Java多线程中,可以使用synchronized关键字来实现线程之间同步互斥,但在JDK1.5中心增加了ReetrantLock类也能达到同样的效果,并且在扩展功能上也更加强大,比如具有嗅探锁定,多路分支通知等功能,而且在使用上也比synchronized更加的灵活
-
调用ReentrantLock对象的lock()方法获取锁,调用unlock()方法释放锁
-
关键字synchronized与wait()和notify()/notifyAll()方法相结合可以实现等待/通知模式,类ReentrantLock也可以实现同样的功能,但需要借助于condition对象。Condition类是在JDK5中出现的技术,使用它有更好的灵活性,比如可以实现多路通知功能,也就是在一个Lock对象里面可以创建多个Condition(即对象监视器)实例,线程对象可以注册在制定的Condition中,从而可以有选择性地进行线程通知,在调度线程上更加灵活
在使用notify()/notifyAll()方法进行通知时,被通知的线程却是由JVM随机选择的。但使用ReetrantLcok结合Condition类是可以实现“选择性通知”,这个功能是非常重要的,而且在Condition类中是默认提供的
而synchronized就相当于整个L从宽对象中只有一个单一的Condition对象,所有线程都注册在它一个对象的身上。线程开始notifyAll()时,需要通知所哦呦的WAITING线程,没有选择权,会出现相当大的效率问题
- Object类中的wait方法相当于Condition类中的await方法
Object类中notify方法相当于Condition类中的signal方法
Object类中的notifyAll方法相当于Condition类中的signalAll
import java.util.Date;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyService{
private Lock lock = new ReentrantLock();
public Condition conditionA = lock.newCondition();
public Condition conditionB = lock.newCondition();
public void awaitA(){
try{
lock.lock();
System.out.println("begin awaitA 时间为" + formateDateTime() + ",ThreadName=" + Thread.currentThread().getName());
conditionA.await();
System.out.println("end awaitA 时间为" + formateDateTime() + ",ThreadName=" + Thread.currentThread().getName());
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void awaitB(){
try{
lock.lock();
System.out.println("begin awaitB 时间为" + formateDateTime() + ",ThreadName=" + Thread.currentThread().getName());
conditionB.await();
System.out.println("end awaitB 时间为" + formateDateTime() + ",ThreadName=" + Thread.currentThread().getName());
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void signalAll_A(){
try{
lock.lock();
System.out.println("signalAll_A 时间为" + formateDateTime() + ",ThreadName=" + Thread.currentThread().getName());
conditionA.signalAll();
}finally {
lock.unlock();
}
}
public void signalAll_B(){
try{
lock.lock();
System.out.println("signalAll_B 时间为" + formateDateTime() + ",ThreadName=" + Thread.currentThread().getName());
conditionB.signalAll();
}finally {
lock.unlock();
}
}
public synchronized String formateDateTime(){
Date nowTime = new Date();
return nowTime.toString();
}
}
public class ThreadA extends Thread {
private MyService service;
public ThreadA(MyService service){
super();
this.service = service;
}
@Override
public void run() {
service.awaitA();
}
}
public class ThreadB extends Thread{
private MyService service;
public ThreadB(MyService service){
super();
this.service = service;
}
@Override
public void run() {
service.awaitB();
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyService service = new MyService();
ThreadA a = new ThreadA(service);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service);
b.setName("B");
b.start();
Thread.sleep(3000);
service.signalAll_A();
}
}
image.png
网友评论