zoukankan      html  css  js  c++  java
  • Synchronized和java.util.concurrent.locks.Lockde区别联系

    1、Lock能够完成几乎所有synchronize的功能,并且具有锁投票,定时锁,可中断等候锁,synchronize是java语言层面的,是内置的关键字,Lock是一个包,synchronize使用的时候JVM可以自动释放,但是Lock需要程序员在finally块中手动释放。

    synchronize在同步资源上,首先线程A获得了该资源的锁,并开始执行,此时他想要操作此资源的程序就必须等待,如果线程A由于某种原因处理常时间的操作状态,那么其他线程就无法得到处理他么的任务,只能无限的等待。所以Lock机制很好的解决了这个问题。

    condition(条件队列或者条件变量) 替代了Object监察方法的使用,condition中的方法如下:

      1. // 造成当前线程在接到信号或被中断之前一直处于等待状态。  
      2. void await()  
      3.   
      4. // 造成当前线程在接到信号、被中断或到达指定等待时间之前一直处于等待状态。  
      5. boolean await(long time, TimeUnit unit)  
      6.   
      7. // 造成当前线程在接到信号、被中断或到达指定等待时间之前一直处于等待状态。  
      8. long awaitNanos(long nanosTimeout)  
      9.   
      10. // 造成当前线程在接到信号之前一直处于等待状态。  
      11. void awaitUninterruptibly()  
      12.   
      13. // 造成当前线程在接到信号、被中断或到达指定最后期限之前一直处于等待状态。  
      14. boolean awaitUntil(Date deadline)  
      15.   
      16. // 唤醒一个等待线程。  
      17. void signal()  
      18.   
      19. // 唤醒所有等待线程。  
      20. void signalAll()

    lock接口的方法:

      1. // 获取锁  
      2. void lock()   
      3.   
      4. // 如果当前线程未被中断,则获取锁  
      5. void lockInterruptibly()   
      6.   
      7. // 返回绑定到此 Lock 实例的新 Condition 实例  
      8. Condition newCondition()   
      9.   
      10. // 仅在调用时锁为空闲状态才获取该锁  
      11. boolean tryLock()   
      12.   
      13. // 如果锁在给定的等待时间内空闲,并且当前线程未被中断,则获取锁  
      14. boolean tryLock(long time, TimeUnit unit)   
      15.   
      16. // 释放锁  
      17. void unlock()  

    package co.DuXieZhe;

    import java.util.concurrent.locks.Condition;  
    import java.util.concurrent.locks.Lock;  
    import java.util.concurrent.locks.ReentrantLock;  
     
    public class BoundedBuffer {  
        final Lock lock = new ReentrantLock();// 锁对象  
        final Condition notFull = lock.newCondition();// 写线程条件  
        final Condition notEmpty = lock.newCondition();// 读线程条件  
     
        final Integer[] items = new Integer[10];// 缓存队列  
        int putptr/* 写索引 */, takeptr/* 读索引 */, count/* 队列中存在的数据个数 */;  
     
        public void put(Integer x) throws InterruptedException {  
            lock.lock();  
            try {  
                while (count == items.length)  
                    // 如果队列满了  
                    notFull.await();// 阻塞写线程  
                items[putptr] = x;// 赋值  
                System.out.println("写入:" + x);  
                if (++putptr == items.length)  
                    putptr = 0;// 如果写索引写到队列的最后一个位置了,那么置为0  
                ++count;// 个数++  
                notEmpty.signal();// 唤醒读线程  
            } finally {  
                lock.unlock();  
            }  
        }  
     
        public Integer take() throws InterruptedException {  
            lock.lock();  
            try {  
                while (count == 0)  
                    // 如果队列为空  
                    notEmpty.await();// 阻塞读线程  
                Integer x = items[takeptr];// 取值  
                System.out.println("读取:" + x);  
                if (++takeptr == items.length)  
                    takeptr = 0;// 如果读索引读到队列的最后一个位置了,那么置为0  
                --count;// 个数--  
                notFull.signal();// 唤醒写线程  
                return x;  
            } finally {  
                lock.unlock();  
            }  
        }  
     
        public static void main(String[] args) {  
            final BoundedBuffer b = new BoundedBuffer();  
     
            new Thread(new Runnable() {  
                public void run() {  
                    int i = 0;  
                    while (true) {  
                        try {  
                            b.put(i++);  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }  
            }).start();  
            new Thread(new Runnable() {  
                public void run() {  
                    while (true) {  
                        try {  
                            b.take();  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }  
            }).start();  
        }  

  • 相关阅读:
    怎么在本地建立一个Maven 项目push到码云(https://git.oschina.net)
    SmartGit(试用期30后),个人继续使用的方法。
    小项目的总结
    Http 协议简略
    servlet 的基础学习
    安卓访问https错误,访问http可以,可能是nginx ssl证书配置有问题
    EF Code first主从表,删除更新从表
    MSSQL 生成唯一自增数据的办法
    select2插件placeholder不显示的问题
    FormData的Ajax提交注意事项
  • 原文地址:https://www.cnblogs.com/xinxianquan/p/8870427.html
Copyright © 2011-2022 走看看