在jdk 1.5 后,Java 引入了lock 锁来替代synchronized ,在使用中,lock锁的使用更加灵活,提供了灵活的 api ,不像传统的synchronized ,一旦进入synchronized中,方法是无法打断的,也就是说有时候会陷入漫长的等待当中;以及进行公平锁的创建(synchronized 说非公平的锁);
一,使用规范:
lock.lock(); try { //doSomeThing } finally { lock.unlock(); } 就相当于: synchronized mehod () { }
二,创建锁对象
ReentrantLock lock =new ReentrantLock();等价于 ReentrantLock lock =new ReentrantLock(false); 创建非公平锁 ReentrantLock lock =new ReentrantLock(true); 创建公平锁
三,公平锁与非公平锁的区别(以公平锁和非公平锁两种方式以多线程方式运行)
class ReenLock implements Runnable{ //创建公平锁 ReentrantLock lock =new ReentrantLock(true); //创建非公平锁 //ReentrantLock lock =new ReentrantLock(); @Override public void run() { while(true) { try { lock.lock(); Thread.sleep(1000); System.out.println(Thread.currentThread().getName()+"拿到了锁"); } catch(Exception e) { e.getStackTrace(); } finally { lock.unlock(); } } } }
公平锁运行结果:
Thread-0拿到了锁 Thread-1拿到了锁 Thread-0拿到了锁 Thread-1拿到了锁 Thread-0拿到了锁
非公平锁运行结果:
Thread-0拿到了锁 Thread-0拿到了锁 Thread-0拿到了锁 Thread-0拿到了锁 Thread-0拿到了锁 Thread-0拿到了锁
结论:
1.公平锁保证了线程的运行顺序,按照先进线程的原则,每一次执行都要进行判断 2.非公平锁执行是无序的,但效率是最高了,synchronized 也是非公平锁,也是推荐使用的
四,tryLock() 使用;
trylock() :尝试去获得锁,返回boolean, ≈在指定的时间内,如果它没有被其他线程占有且没有被打断,则获得这把锁;
我们可以在没有获得这把锁的时候,去执行程序其他的逻辑;
class TryLock implements Runnable{ //创建非公平锁 ReentrantLock lock =new ReentrantLock(); @Override public void run() { boolean tryLock =false; try { //尝试获取锁,如果能得到锁,则立即返回true tryLock=lock.tryLock(5, TimeUnit.SECONDS); if(tryLock) { Thread.sleep(10000); System.out.println(Thread.currentThread().getName()+"拿到了锁"); } else {
//没有拿到锁 System.out.println("号外:有个人长时间占这锁"); } } catch(Exception e) { e.getStackTrace(); } finally {
//必须进行判断,以防拿不到锁去释放会报错 if(tryLock) {lock.unlock();}; } } }
五, 要说lock 锁代替了synchronized ,那么condition 代替了object 中 wait notify notifyAll 等方法;
演示使用lock 和condition 进行生产者与消费者的演示
/** * use jdk1.5 的reentranLock 与condition 进行provider and consumer * lock 锁代替了synchronized * @author iscys * */ public class LockAndConditionProviderAndConsumer { public static void main(String[] args) { NewLock samp =new NewLock(); PrividerNewHandler provider =new PrividerNewHandler(samp); ConsumerNewHandler consumer =new ConsumerNewHandler(samp); new Thread(provider).start(); new Thread(provider).start(); new Thread(consumer).start(); new Thread(consumer).start(); new Thread(consumer).start(); new Thread(consumer).start(); } } class PrividerNewHandler implements Runnable{ private NewLock newl; PrividerNewHandler(NewLock newl){ this.newl=newl; } @Override public void run() { try { while(true) newl.provider(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class ConsumerNewHandler implements Runnable{ private NewLock newl; ConsumerNewHandler(NewLock newl){ this.newl=newl; } @Override public void run() { try { while(true) newl.consumer(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class NewLock{ private int max =10; private int init=0; ReentrantLock lock =new ReentrantLock(); Condition p =lock.newCondition(); Condition c=lock.newCondition(); void provider(){ lock.lock(); try { while(init>=max) { p.await(); } Thread.sleep(100); init++; System.out.println(Thread.currentThread().getName()+"生产到了"+init); c.signalAll(); } catch(Exception e) {} finally {lock.unlock();} } void consumer(){ lock.lock(); try { while(init==0) { c.await(); } Thread.sleep(100); init--; System.err.println(Thread.currentThread().getName()+"消费到了"+init); p.signalAll(); } catch(Exception e) {} finally {lock.unlock();} } }
原创打造,多多指教