今日整理

现在整理一下吧

Array BlockingQueue 手动实现一下吧

首先构造

//有如下定义
/** The queued items  */  
private final E[] items;  
/** items index for next take, poll or remove */  
private int takeIndex;  
/** items index for next put, offer, or add. */  
private int putIndex;  
/** Number of items in the queue */  
private int count;  

/* 
 * Concurrency control uses the classic two-condition algorithm 
 * found in any textbook. 
 */  

/** Main lock guarding all access */  
private final ReentrantLock lock;  
/** Condition for waiting takes */  
private final Condition notEmpty;  
/** Condition for waiting puts */  
private final Condition notFull;
//初始化capacity 初始化队列容量 锁是否公平 
public ArrayBlockingQueue(int capacity, boolean fair) {  

       if (capacity <= 0)  
           throw new IllegalArgumentException();  
       this.items = (E[]) new Object[capacity];  
       lock = new ReentrantLock(fair);  
       notEmpty = lock.newCondition();  
       notFull =  lock.newCondition();  
   }
//put方法
public void put(E e) throws InterruptedException {  
        //不能存放 null  元素  
        if (e == null) throw new NullPointerException();  
        final E[] items = this.items;   //数组队列  
        final ReentrantLock lock = this.lock;  
        //加锁  
        lock.lockInterruptibly();  
        try {  
            try {  
                //当队列满时,调用notFull.await()方法,使该线程阻塞。  
                //直到take掉某个元素后,调用notFull.signal()方法激活该线程。  
                while (count == items.length)  
                    notFull.await();  
            } catch (InterruptedException ie) {  
                notFull.signal(); // propagate to non-interrupted thread  
                throw ie;  
            }  
            //把元素 e 插入到队尾  
            insert(e);  
        } finally {  
            //解锁  
            lock.unlock();  
        }  
    }  
//insert 方法
 private void insert(E x) {  
      items[putIndex] = x;    
//下标加1或者等于0  
      putIndex = inc(putIndex);  
      ++count;  //计数加1  
//若有take()线程陷入阻塞,则该操作激活take()线程,继续进行取元素操作。  
//若没有take()线程陷入阻塞,则该操作无意义。  
      notEmpty.signal();  
  }  

**  
   * Circularly increment i.  
   */  
  final int inc(int i) {  
//此处可以看到使用了循环队列  
      return (++i == items.length)? 0 : i;  
  }  

//take方法
public E take() throws InterruptedException {  
        final ReentrantLock lock = this.lock;  
        lock.lockInterruptibly();  //加锁  
        try {  
            try {  
                //当队列空时,调用notEmpty.await()方法,使该线程阻塞。  
                //直到take掉某个元素后,调用notEmpty.signal()方法激活该线程。  
                while (count == 0)  
                    notEmpty.await();  
            } catch (InterruptedException ie) {  
                notEmpty.signal(); // propagate to non-interrupted thread  
                throw ie;  
            }  
            //取出队头元素  
            E x = extract();  
            return x;  
        } finally {  
            lock.unlock();  //解锁  
        }  
    }
  private E extract() {  
        final E[] items = this.items;  
        E x = items[takeIndex];  
        items[takeIndex] = null;  
        takeIndex = inc(takeIndex);  
        --count;  
        notFull.signal();  
        return x;  
    }  

设计模式方面

//单例
public class Singleton {
     private static final Singleton singleton = new Singleton();
//限制产生多个对象
     private Singleton(){
     }
     //通过该方法获得实例对象
     public static Singleton getSingleton(){
             return singleton;
     }  
     //类中其他方法,尽量是static
     public static void doSomething(){
     }
}

命令模式
将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。
策略模式
定义一组算法,将每个算法都封装起来,并且使它们之间可以互换

redis和memcache的区别

redis的数据格式多种 kv list 而memcache只能kv ,redis 必要时可以存入硬盘

Map的底层实现

https://www.cnblogs.com/chengxiao/p/6059914.html
http://www.importnew.com/16301.html
http://blog.csdn.net/wushiwude/article/details/76041751

ConcurrentMap

https://www.cnblogs.com/ITtangtang/p/3948786.html

ThreadPoolExecutor

http://blog.csdn.net/pozmckaoddb/article/details/51478017

GC详解

https://zhuanlan.zhihu.com/p/23102625
http://www.infoq.com/cn/articles/why-jvm-need-gc

JVM详解

http://blog.csdn.net/wuzhilon88/article/details/49201891

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值