yuyee

          2010年10月26日 #

          適配器

          適配器模式:將一個現(xiàn)有類實現(xiàn)的功能接口轉(zhuǎn)變?yōu)榭蛻粝M慕涌?/span>

          場景:你想使用一個已經(jīng)存在的類,但是這個類的接口不符合需求,所以需要適配

          2中實現(xiàn):一種是繼承,一種是委托,先來看看繼承

             

          第一步:系統(tǒng)現(xiàn)有功能

          package com.google.desginpattern.adapter;
          /**
           * 現(xiàn)有系統(tǒng)提供的功能
           * 
           * 
          @author Administrator
           * 
           
          */
          public class BMWCar {
          public void quickDriver() {
          System.out.println(
          "寶馬太快");
          }
          }

           

          第二步:客戶需要的接口

          package com.google.desginpattern.adapter;
          /**
           * 客戶需要的接口
           * 
          @author Administrator
           *
           
          */
          public interface Car {
          public void driver();
          public void brake();
          }

           

          第三步:實現(xiàn)客戶需要的功能

          package com.google.desginpattern.adapter;
          /**
           * 匹配客戶需求的實現(xiàn)
           * 
          @author Administrator
           *
           
          */
          public class CarAdapter extends BMWCar implements Car {
          @Override
          public void brake() {
          System.out.println(
          "剎車");
          }
          @Override
          public void driver() {
          quickDriver();
          }
          }

           

          測試類:

           

          package com.google.desginpattern.adapter;


          public class Test {

          public static void main(String[] args) {


          Car car 
          = new CarAdapter();

          car.brake();

          car.driver();

          }


          }

          輸出:

          剎車

          寶馬太快

          如果是委托的方式,改寫adapter

          package com.google.desginpattern.adapter;
          /**
           * 匹配客戶需求的實現(xiàn)
           * 
           * 
          @author Administrator
           * 
           
          */
          public class CarAdapter implements Car {
          private BMWCar car;
          @Override
          public void brake() {
          System.out.println(
          "剎車");
          }
          @Override
          public void driver() {
          car.quickDriver();
          }
          public BMWCar getCar() {
          return car;
          }
          public void setCar(BMWCar car) {
          this.car = car;
          }
          }

           

          posted @ 2010-11-29 22:28 羔羊| 編輯 收藏

          裝飾器


          裝飾器:裝飾器模式主要用于系統(tǒng)擴(kuò)張功能用,在系統(tǒng)原有的功能上,擴(kuò)展出其他的功能,JDKIO包用到很多,比如datainputstream之類,需要用其他流進(jìn)行構(gòu)造的上層類,符合面向?qū)ο笤O(shè)計的開閉原則

          下面我來寫個例子:

          首先,寫一個Car模版,定義基本屬性及行為功能driver

          package com.google.desginpattern.decoration;
          //其實這是個模版
          public abstract class Car {
          private int spreed;
          public int getSpreed() {
          return spreed;
          }
          public void setSpreed(int spreed) {
          this.spreed = spreed;
          }
          public abstract void driver();
          }

           

          第二步:具體車比如寶馬,這是目前系統(tǒng)中這個類能提供的功能

          package com.google.desginpattern.decoration;
          //目前系統(tǒng)中此類的功能
          public class BMWCar extends Car {
          @Override
          public void driver() {
          System.out.println(
          "我開著寶馬車");
          }
          }

           

          現(xiàn)在我想在這個類上擴(kuò)展出其他功能,比如:泡妞

          第三步:定義一個裝飾模板,為什么給他定義個模板呢~因為可以給這個BMWCar類裝飾很不同的功能,不只泡妞一個~

          繼承Car父類,覆蓋driver功能,調(diào)用Car引用完成driver功能

          package com.google.desginpattern.decoration;
          //裝飾器父類
          public abstract class DecorationCar extends Car {
          // 引入car
          private Car car;
          @Override
          public void driver() {
          car.driver();
          // 調(diào)用此car來完成裝飾器的功能
          }
          public Car getCar() {
          return car;
          }
          public void setCar(Car car) {
          this.car = car;
          }
          }

           

          第四步:具體的裝飾功能,添加一個構(gòu)造函數(shù),參數(shù)為Car,為裝飾父類Car引用賦值,其實就是原來具體的功能類,回想下IO包里經(jīng)常new的代碼段就明白~~

          package com.google.desginpattern.decoration;
          //具體的裝飾類,添加額外的泡妞功能
          public class DecorationBMWCar extends DecorationCar {
          public DecorationBMWCar(Car car) {
          super.setCar(car);
          }
          @Override
          public void driver() {
          // TODO Auto-generated method stub
          super.driver();// 調(diào)用原來的功能
          System.out.println("泡妞");// 添加額外的功能
          }
          }

           

          測試類:構(gòu)造的方法很像IO包里的流

          package com.google.desginpattern.decoration;
          public class Test {
          public static void main(String[] args) {
          Car car 
          = new DecorationBMWCar(new BMWCar());
          car.driver();
          }
          }

           

          輸出:

          我開著寶馬車

          泡妞

          posted @ 2010-11-29 21:36 羔羊| 編輯 收藏

          IOC初始化和互相引用解決

               摘要: 觀察IOC中容器初始化某個Bean順序,現(xiàn)先一個JAVABean類,看看控制臺輸出:package com.google.aop.exception.ioc; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory...  閱讀全文

          posted @ 2010-11-10 16:27 羔羊| 編輯 收藏

          AbstractQueuedSynchronizer看看


          API中的解釋:為實現(xiàn)依賴于先進(jìn)先出 (FIFO) 等待隊列的阻塞鎖定和相關(guān)同步器(信號量、事件,等等)提供一個框架。此類的設(shè)計目標(biāo)是成為依靠單個原子 int 值來表示狀態(tài)的大多數(shù)同步器的一個有用基礎(chǔ)。子類必須定義更改此狀態(tài)的受保護(hù)方法,并定義哪種狀態(tài)對于此對象意味著被獲取或被釋放。假定這些條件之后,此類中的其他方法就可以實現(xiàn)所有排隊和阻塞機(jī)制。子類可以維護(hù)其他狀態(tài)字段,但只是為了獲得同步而只追蹤使用 getState()getState()getState()setState(int)compareAndSetState(int, int) 方法來操作以原子方式更新的 int 值。
          此類采用模板模式設(shè)計,此類為一個抽象類,但是沒抽象方法,每個sync子類需要實現(xiàn)5個受保護(hù)的方法

          這個5個方法在AbstractQueuedSynchronizer 都拋出throw new UnsupportedOperationException();
          AbstractQueuedSynchronizer 中有3個屬性:主要聲明一個狀態(tài)和一個wait queue,通過

          wait queue中Node 為一個雙向鏈表,需要去理解Node中幾個靜態(tài)字段值的意義,下面為他的源碼:
          static final class Node {
                  /** waitStatus value to indicate thread has cancelled */
                  static final int CANCELLED =  1;
                  /** waitStatus value to indicate successor's thread needs unparking */
                  static final int SIGNAL    = -1;
                  /** waitStatus value to indicate thread is waiting on condition */
                  static final int CONDITION = -2;
                  /** Marker to indicate a node is waiting in shared mode */
                  static final Node SHARED = new Node();
                  /** Marker to indicate a node is waiting in exclusive mode */
                  static final Node EXCLUSIVE = null;

                  /**
                   * Status field, taking on only the values:
                   *   SIGNAL:     The successor of this node is (or will soon be)
                   *               blocked (via park), so the current node must
                   *               unpark its successor when it releases or
                   *               cancels. To avoid races, acquire methods must
                   *               first indicate they need a signal,
                   *               then retry the atomic acquire, and then,
                   *               on failure, block.
                   *   CANCELLED:  This node is cancelled due to timeout or interrupt.
                   *               Nodes never leave this state. In particular,
                   *               a thread with cancelled node never again blocks.
                   *   CONDITION:  This node is currently on a condition queue.
                   *               It will not be used as a sync queue node until
                   *               transferred. (Use of this value here
                   *               has nothing to do with the other uses
                   *               of the field, but simplifies mechanics.)
                   *   0:          None of the above
                   *
                   * The values are arranged numerically to simplify use.
                   * Non-negative values mean that a node doesn't need to
                   * signal. So, most code doesn't need to check for particular
                   * values, just for sign.
                   *
                   * The field is initialized to 0 for normal sync nodes, and
                   * CONDITION for condition nodes.  It is modified only using
                   * CAS.
                   */
                  volatile int waitStatus;

                  /**
                   * Link to predecessor node that current node/thread relies on
                   * for checking waitStatus. Assigned during enqueing, and nulled
                   * out (for sake of GC) only upon dequeuing.  Also, upon
                   * cancellation of a predecessor, we short-circuit while
                   * finding a non-cancelled one, which will always exist
                   * because the head node is never cancelled: A node becomes
                   * head only as a result of successful acquire. A
                   * cancelled thread never succeeds in acquiring, and a thread only
                   * cancels itself, not any other node.
                   */
                  volatile Node prev;

                  /**
                   * Link to the successor node that the current node/thread
                   * unparks upon release. Assigned once during enqueuing, and
                   * nulled out (for sake of GC) when no longer needed.  Upon
                   * cancellation, we cannot adjust this field, but can notice
                   * status and bypass the node if cancelled.  The enq operation
                   * does not assign next field of a predecessor until after
                   * attachment, so seeing a null next field does not
                   * necessarily mean that node is at end of queue. However, if
                   * a next field appears to be null, we can scan prev's from
                   * the tail to double-check.
                   */
                  volatile Node next;

                  /**
                   * The thread that enqueued this node.  Initialized on
                   * construction and nulled out after use.
                   */
                  volatile Thread thread;

                  /**
                   * Link to next node waiting on condition, or the special
                   * value SHARED.  Because condition queues are accessed only
                   * when holding in exclusive mode, we just need a simple
                   * linked queue to hold nodes while they are waiting on
                   * conditions. They are then transferred to the queue to
                   * re-acquire. And because conditions can only be exclusive,
                   * we save a field by using special value to indicate shared
                   * mode.
                   */
                  Node nextWaiter;

                  /**
                   * Returns true if node is waiting in shared mode
                   */
                  final boolean isShared() {
                      return nextWaiter == SHARED;
                  }

                  /**
                   * Returns previous node, or throws NullPointerException if
                   * null.  Use when predecessor cannot be null.
                   * @return the predecessor of this node
                   */
                  final Node predecessor() throws NullPointerException {
                      Node p = prev;
                      if (p == null)
                          throw new NullPointerException();
                      else
                          return p;
                  }

                  Node() {    // Used to establish initial head or SHARED marker
                  }

                  Node(Thread thread, Node mode) {     // Used by addWaiter
                      this.nextWaiter = mode;
                      this.thread = thread;
                  }

                  Node(Thread thread, int waitStatus) { // Used by Condition
                      this.waitStatus = waitStatus;
                      this.thread = thread;
                  }


              }
          獲取鎖定調(diào)用的方法,其實這個方法是阻塞的:
            public final void acquire(int arg) {
                  if (!tryAcquire(arg) &&
                      acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
                      selfInterrupt();
              }
          如果獲取不成功則調(diào)用如下方法:
             final boolean acquireQueued(final Node node, int arg) {
                  try {
                      boolean interrupted = false;
                      for (;;) {
                          final Node p = node.predecessor();
                          if (p == head && tryAcquire(arg)) {//當(dāng)節(jié)點是頭節(jié)點且獨占時才返回
                              setHead(node);
                              p.next = null; // help GC
                              return interrupted;
                          }
                          if (shouldParkAfterFailedAcquire(p, node) &&
                              parkAndCheckInterrupt())//阻塞并判斷是否打斷,其實這個判斷才是自旋鎖真正的猥瑣點,
          意思是如果你的前繼節(jié)點不是head,而且當(dāng)你的前繼節(jié)點狀態(tài)是Node.SIGNAL時,你這個線程將被park(),直到另外的線程release時,發(fā)現(xiàn)head.next是你這個node時,才unpark,你才能繼續(xù)循環(huán)并獲取鎖
                              interrupted = true;
                      }
          shouldParkAfterFailedAcquire這個方法刪除所有waitStatus>0也就是CANCELLED狀態(tài)的Node,并設(shè)置前繼節(jié)點為signal
           private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
                  int s = pred.waitStatus;
                  if (s < 0)
                      /*
                       * This node has already set status asking a release
                       * to signal it, so it can safely park
                       */
                      return true;
                  if (s > 0) {
                      /*
                       * Predecessor was cancelled. Skip over predecessors and
                       * indicate retry.
                       */
             do {
          node.prev = pred = pred.prev;
             } while (pred.waitStatus > 0);
             pred.next = node;
          }
                  else
                      /*
                       * Indicate that we need a signal, but don't park yet. Caller
                       * will need to retry to make sure it cannot acquire before
                       * parking.
                       */
                      compareAndSetWaitStatus(pred, 0, Node.SIGNAL);
                  return false;
              }


          使用LockSupport.park(this),禁用當(dāng)前線程
          private final boolean parkAndCheckInterrupt() {
                  LockSupport.park(this);//block
                  return Thread.interrupted();
              }
          釋放鎖:
              public final boolean release(int arg) {
                  if (tryRelease(arg)) {
                      Node h = head;
                      if (h != null && h.waitStatus != 0)
                          unparkSuccessor(h);//unblock
                      return true;
                  }
                  return false;
              }
          private void unparkSuccessor(Node node) {
                  /*
                   * Try to clear status in anticipation of signalling.  It is
                   * OK if this fails or if status is changed by waiting thread.
                   */
                  compareAndSetWaitStatus(node, Node.SIGNAL, 0);

                  /*
                   * Thread to unpark is held in successor, which is normally
                   * just the next node.  But if cancelled or apparently null,
                   * traverse backwards from tail to find the actual
                   * non-cancelled successor.
                   */
                  Node s = node.next;
                  if (s == null || s.waitStatus > 0) {
                      s = null;
                      for (Node t = tail; t != null && t != node; t = t.prev)
                          if (t.waitStatus <= 0)
                              s = t;
                  }
                  if (s != null)
                      LockSupport.unpark(s.thread);
              }


                  } catch (RuntimeException ex) {
                      cancelAcquire(node);
                      throw ex;
                  }
              }
            

          看下ReentrantLock鎖中sync的實現(xiàn):
           static abstract class Sync extends AbstractQueuedSynchronizer {
                  private static final long serialVersionUID = -5179523762034025860L;

                  /**
                   * Performs {@link Lock#lock}. The main reason for subclassing
                   * is to allow fast path for nonfair version.
                   */
                  abstract void lock();

                  /**
                   * Performs non-fair tryLock.  tryAcquire is
                   * implemented in subclasses, but both need nonfair
                   * try for trylock method.
                   */
                  final boolean nonfairTryAcquire(int acquires) {
                      final Thread current = Thread.currentThread();
                      int c = getState();
                      if (c == 0) {
                          if (compareAndSetState(0, acquires)) {
                              setExclusiveOwnerThread(current);
                              return true;
                          }
                      }
                      else if (current == getExclusiveOwnerThread()) {
                          int nextc = c + acquires;
                          if (nextc < 0) // overflow
                              throw new Error("Maximum lock count exceeded");
                          setState(nextc);
                          return true;
                      }
                      return false;
                  }

                  protected final boolean tryRelease(int releases) {
                      int c = getState() - releases;
                      if (Thread.currentThread() != getExclusiveOwnerThread())
                          throw new IllegalMonitorStateException();
                      boolean free = false;
                      if (c == 0) {
                          free = true;
                          setExclusiveOwnerThread(null);
                      }
                      setState(c);
                      return free;
                  }

                  protected final boolean isHeldExclusively() {
                      // While we must in general read state before owner,
                      // we don't need to do so to check if current thread is owner
                      return getExclusiveOwnerThread() == Thread.currentThread();
                  }

                  final ConditionObject newCondition() {
                      return new ConditionObject();
                  }

                  // Methods relayed from outer class

                  final Thread getOwner() {
                      return getState() == 0 ? null : getExclusiveOwnerThread();
                  }

                  final int getHoldCount() {
                      return isHeldExclusively() ? getState() : 0;
                  }

                  final boolean isLocked() {
                      return getState() != 0;
                  }

                  /**
                   * Reconstitutes this lock instance from a stream.
                   * @param s the stream
                   */
                  private void readObject(java.io.ObjectInputStream s)
                      throws java.io.IOException, ClassNotFoundException {
                      s.defaultReadObject();
                      setState(0); // reset to unlocked state
                  }
              }
          非公平規(guī)則下nonfairTryAcquire,獲取當(dāng)前鎖的state,通過CAS原子操作設(shè)置為1,并將當(dāng)前線程設(shè)置為獨占線程,如果當(dāng)前線程已經(jīng)拿了鎖,則state增加1
          公平鎖中 有如下判斷:
          if (isFirst(current) &&//判斷頭元素
                              compareAndSetState(0, acquires)) {
                              setExclusiveOwnerThread(current);
                              return true;
                          }
          在獲取鎖步驟:
          1.調(diào)用tryAcquire來獲取,如果失敗,則進(jìn)入2
          2.調(diào)用addWaiter,以獨占模式將node放到tail位置
          3.調(diào)用acquireQueued方法,此方法阻塞,直到node的pre為head,并成功獲取鎖定,也可能存在阻塞并打斷情況
          釋放鎖的步驟:
          1.放棄排他鎖持有權(quán)
          2.unpark 節(jié)點的下一個blocked節(jié)點

          公平鎖與非公平鎖:從代碼上看,非公平鎖是讓當(dāng)前線程優(yōu)先獨占,而公平鎖則是讓等待時間最長的線程優(yōu)先,非公平的可能讓其他線程沒機(jī)會執(zhí)行,而公平的則可以讓等待時間最長的先執(zhí)行,但是性能上會差點

          posted @ 2010-11-09 15:30 羔羊| 編輯 收藏

          linkedhashmap看看

          linkedhashmap繼承自hashmap,他的底層維護(hù)的一個鏈表,    private transient Entry<K,V> header 來記錄元素的插入順序和訪問順序;
          hashmap的構(gòu)造函數(shù)中調(diào)用init()方法,而linkedhashmap中重寫了init(),將head元素初始化
             void init() {
                  header = new Entry<K,V>(-1, null, null, null);
                  header.before = header.after = header;
              }

          private final boolean accessOrder這個屬性表示是否要根據(jù)訪問順序改變線性結(jié)構(gòu)
          在linkedhashmap中改寫了hashmap的get()方法,增加了 e.recordAccess(this),這個方法主要是根據(jù)accessOrder的值判斷是否需要實現(xiàn)LRU,
           void recordAccess(HashMap<K,V> m) {
                      LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
                      if (lm.accessOrder) {
                          lm.modCount++;
                          remove();
                          addBefore(lm.header);
                      }
                  }

          addBefore這個方法是把剛訪問的元素放到head的前面
           private void addBefore(Entry<K,V> existingEntry) {
                      after  = existingEntry;
                      before = existingEntry.before;
                      before.after = this;
                      after.before = this;
                  }
          put方法繼承自hashmap,hashmap預(yù)留了 e.recordAccess(this)這個方法:
               public V put(K key, V value) {
                  if (key == null)
                      return putForNullKey(value);
                  int hash = hash(key.hashCode());
                  int i = indexFor(hash, table.length);
                  for (Entry<K,V> e = table[i]; e != null; e = e.next) {
                      Object k;
                      if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                          V oldValue = e.value;
                          e.value = value;
                          e.recordAccess(this);
                          return oldValue;
                      }
                  }

                  modCount++;
                  addEntry(hash, key, value, i);
                  return null;
              }

          并通過重寫 addEntry(hash, key, value, i)這個方法,實現(xiàn)LRU中的刪除動作:
              void addEntry(int hash, K key, V value, int bucketIndex) {
                  createEntry(hash, key, value, bucketIndex);

                  // Remove eldest entry if instructed, else grow capacity if appropriate
                  Entry<K,V> eldest = header.after;//找到最老的元素,這個在addBefore里確定,初次賦值是當(dāng)只有一個head時候,你插入一個元素
                  if (removeEldestEntry(eldest)) {//這個是受保護(hù)的方法,需要自己制定刪除策略,比如size() > 最大容量,可自己實現(xiàn),默認(rèn)為false,也就是不開啟
                      removeEntryForKey(eldest.key);
                  } else {
                      if (size >= threshold)
                          resize(2 * table.length);
                  }
              }
          自己重寫這個方法,指定刪除策略:
           protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
                  return false;
              }
          因此,可用linkedhashmap 構(gòu)建一個基于LRU算法的緩存。


          package com.google.study.cache;

          import java.util.LinkedHashMap;
          import java.util.concurrent.locks.ReentrantLock;

          public class SimpleCache<K, V> extends LinkedHashMap<K, V> {

          private int maxCapacity;
          private ReentrantLock lock = new ReentrantLock();

          public SimpleCache(int maxCapacity, float load_factory) {
          super(maxCapacity, load_factory, true);
          this.maxCapacity = maxCapacity;
          }

          public int getMaxCapacity() {
          return maxCapacity;
          }

          public void setMaxCapacity(int maxCapacity) {
          this.maxCapacity = maxCapacity;
          }

          @Override
          protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
          // TODO Auto-generated method stub
          return super.removeEldestEntry(eldest);
          }

          public V get(Object key) {
          lock.lock();
          try {
          return super.get(key);
          } finally {
          lock.unlock();
          }

          }

          public V put(K k, V v) {
          lock.lock();
          try {
          return super.put(k, v);
          } finally {
          lock.unlock();
          }
          }

          @Override
          public void clear() {
          lock.lock();
          try {
          super.clear();
          } finally {
          lock.unlock();
          }
          }

          @Override
          public int size() {

          lock.lock();
          try {
          return super.size();
          } finally {
          lock.unlock();
          }
          }

          }



          posted @ 2010-11-08 23:14 羔羊| 編輯 收藏

          IOC監(jiān)管Bean

          1.InitializingBean接口,在初始化Bean時容器會調(diào)用前者的afterPropertiesSet()方法

          2.DisposableBean接口,在析構(gòu)Bean時容器會調(diào)用destroy()方法

          3.BeanFactoryAware接口,當(dāng)它被BeanFactory創(chuàng)建后,它會擁有一個指向創(chuàng)建它的BeanFactory的引用

          4.BeanPostProcessor接口,這個接口兩個方法,postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName) 在其他Bean構(gòu)造前后執(zhí)行

          5.BeanFactoryPostProcessor接口,Spring IoC容器允許BeanFactoryPostProcessor在容器實際實例化任何其它的bean之前讀取配置元數(shù)據(jù),并有可能修改它

          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class A implements BeanPostProcessor, InitializingBean,
          BeanFactoryAware, BeanFactoryPostProcessor,DisposableBean {

          public A() {
          System.out.println("Class A");
          }
          private void init(){
          System.out.println("Class A init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          // TODO Auto-generated method stub
          System.out.println("Class A afterPropertiesSet()");

          }

          @Override
          public void destroy() throws Exception {
          System.out.println("Class A destroy()");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A setBeanFactory()");
          }

          @Override
          public Object postProcessAfterInitialization(Object bean, String beanName)
          throws BeansException {
          System.out.println("Class A postProcessAfterInitialization");
          return null;
          }

          @Override
          public Object postProcessBeforeInitialization(Object bean, String beanName)
          throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A postProcessBeforeInitialization");
          return null;
          }


          @Override
          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A postProcessBeanFactory");
          }

          }
          執(zhí)行結(jié)果:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          創(chuàng)建一個B類,由A來監(jiān)管B類,B實現(xiàn) InitializingBean,BeanFactoryAware,BeanFactoryPostProcessor
          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class B implements InitializingBean, BeanFactoryAware,
          BeanFactoryPostProcessor {

          public B() {
          System.out.println("Class B");
          }

          public void init() {
          System.out.println("Class B init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          System.out.println("Class B afterPropertiesSet");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          System.out.println("Class B beanFactory");

          }

          @Override
          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class B postProcessBeanFactory");
          }

          }
          執(zhí)行結(jié)果:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class B
          Class B beanFactory
          Class B afterPropertiesSet
          Class B init
          Class A postProcessBeanFactory
          Class B postProcessBeanFactory

          可以看出A并沒有監(jiān)管B類,也就是沒調(diào)用BeanPostProcessor這個接口的2個方法
          再來去掉B上的BeanFactoryPostProcessor接口
          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class B implements InitializingBean,
          BeanFactoryAware{

          public B() {
          System.out.println("Class B");
          }

          public void init() {
          System.out.println("Class B init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          System.out.println("Class B afterPropertiesSet");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          System.out.println("Class B beanFactory");

          }

          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class B postProcessBeanFactory");
          }


          }
          執(zhí)行輸出:Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          2010-11-3 21:33:31 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
          信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8916a2: defining beans [A,B]; root of factory hierarchy
          Class B
          Class B beanFactory
          Class A postProcessBeforeInitialization
          2010-11-3 21:33:31 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
          信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8916a2: defining beans [A,B]; root of factory hierarchy
          Class A destroy()
          Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'B' defined in class path resource [icoContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
          at java.security.AccessController.doPrivileged(Native Method)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
          at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
          at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
          at com.google.springioctest.Test.main(Test.java:8)
          Caused by: java.lang.NullPointerException
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1393)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1375)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
          ... 15 more
          拋異常了。。。。。
          原因是A類里的postProcessBeforeInitialization,postProcessAfterInitialization2個方法沒有返回bean,修改下
          執(zhí)行輸出:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          2010-11-3 21:37:10 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
          信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1193779: defining beans [A,B]; root of factory hierarchy
          Class B
          Class B beanFactory
          Class A postProcessBeforeInitialization
          Class B afterPropertiesSet
          Class B init
          Class A postProcessAfterInitialization
          在B類被初始化之后,也就是調(diào)用afterPropertiesSet之前那段時間,屬性初始化完成后,進(jìn)行了回調(diào),控制B類

          注意:在寫被監(jiān)控的Bean的時候,不能實現(xiàn)BeanFactoryPostProcessor這個接口,沒看源碼,其實也不知道是什么原因,哈哈,只能硬記了

          posted @ 2010-11-03 17:04 羔羊| 編輯 收藏

          嵌套事務(wù)

          在所有使用 spring 的應(yīng)用中, 聲明式事務(wù)管理可能是使用率最高的功能了, 但是, 從我觀察到的情況看, 絕大多數(shù)人并不能深刻理解事務(wù)聲明中不同事務(wù)傳播屬性配置的的含義, 讓我們來看一下TransactionDefinition 接口中的定義 ,在 spring 中一共定義了六種事務(wù)傳播屬性, 如果你覺得看起來不夠直觀, 那么我來轉(zhuǎn)貼一個滿大街都有的翻譯 
          PROPAGATION_REQUIRED -- 支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個事務(wù)。這是最常見的選擇。 
          PROPAGATION_SUPPORTS 
          -- 支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就以非事務(wù)方式執(zhí)行。 
          PROPAGATION_MANDATORY 
          -- 支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就拋出異常。 
          PROPAGATION_REQUIRES_NEW 
          -- 新建事務(wù),如果當(dāng)前存在事務(wù),把當(dāng)前事務(wù)掛起。 
          PROPAGATION_NOT_SUPPORTED 
          -- 以非事務(wù)方式執(zhí)行操作,如果當(dāng)前存在事務(wù),就把當(dāng)前事務(wù)掛起。 
          PROPAGATION_NEVER 
          -- 以非事務(wù)方式執(zhí)行,如果當(dāng)前存在事務(wù),則拋出異常。 
          PROPAGATION_NESTED 
          -- 如果當(dāng)前存在事務(wù),則在嵌套事務(wù)內(nèi)執(zhí)行。如果當(dāng)前沒有事務(wù),則進(jìn)行與PROPAGATION_REQUIRED類似的操作。 
          前六個策略類似于EJB CMT,第七個(PROPAGATION_NESTED)是Spring所提供的一個特殊變量。 
          它要求事務(wù)管理器或者使用JDBC 
          3.0 Savepoint API提供嵌套事務(wù)行為(如Spring的DataSourceTransactionManager) 

          在我所見過的誤解中, 最常見的是下面這種: 
          假如有兩個業(yè)務(wù)接口 ServiceA 和 ServiceB, 其中 ServiceA 中有一個方法實現(xiàn)如下 
          /** 
          * 事務(wù)屬性配置為 PROPAGATION_REQUIRED 
          */
           
          void methodA() 
          // 調(diào)用 ServiceB 的方法 
          ServiceB.methodB(); 
          }
           
          那么如果 ServiceB 的 methodB  如果配置了事務(wù), 就必須配置為 PROPAGATION_NESTED 


          這種想法可能害了不少人, 認(rèn)為 Service 之間應(yīng)該避免互相調(diào)用, 其實根本不用擔(dān)心這點,PROPAGATION_REQUIRED 已經(jīng)說得很明白, 
          如果當(dāng)前線程中已經(jīng)存在事務(wù), 方法調(diào)用會加入此事務(wù), 果當(dāng)前沒有事務(wù),就新建一個事務(wù), 所以 ServiceB#methodB() 的事務(wù)只要遵循最普通的規(guī)則配置為 PROPAGATION_REQUIRED 即可, 如果 ServiceB#methodB (我們稱之為內(nèi)部事務(wù), 為下文打下基礎(chǔ)) 拋了異常, 那么 ServiceA#methodA(我們稱之為外部事務(wù)) 如果沒有特殊配置此異常時事務(wù)提交 (即 +MyCheckedException的用法), 那么整個事務(wù)是一定要 rollback 的, 什么 Service 只能調(diào) Dao 之類的言論純屬無稽之談, spring 只負(fù)責(zé)配置了事務(wù)屬性方法的攔截, 它怎么知道你這個方法是在 Service 還是 Dao 里 ? 

          最容易弄混淆的其實是 PROPAGATION_REQUIRES_NEW 和 PROPAGATION_NESTED, 那么這兩種方式又有何區(qū)別呢? 我簡單的翻譯一下 Juergen Hoeller 的話 : 
          PROPAGATION_REQUIRES_NEW 啟動一個新的, 不依賴于環(huán)境的 "內(nèi)部" 事務(wù). 這個事務(wù)將被完全 commited 或 rolled back 而不依賴于外部事務(wù), 它擁有自己的隔離范圍, 自己的鎖, 等等. 當(dāng)內(nèi)部事務(wù)開始執(zhí)行時, 外部事務(wù)將被掛起, 內(nèi)務(wù)事務(wù)結(jié)束時, 外部事務(wù)將繼續(xù)執(zhí)行. 

              另一方面, PROPAGATION_NESTED 開始一個 "嵌套的" 事務(wù),  它是已經(jīng)存在事務(wù)的一個真正的子事務(wù). 潛套事務(wù)開始執(zhí)行時,  它將取得一個 savepoint. 如果這個嵌套事務(wù)失敗, 我們將回滾到此 savepoint. 潛套事務(wù)是外部事務(wù)的一部分, 只有外部事務(wù)結(jié)束后它才會被提交. 

              由此可見, PROPAGATION_REQUIRES_NEW 和 PROPAGATION_NESTED 的最大區(qū)別在于, PROPAGATION_REQUIRES_NEW 完全是一個新的事務(wù), 而 PROPAGATION_NESTED 則是外部事務(wù)的子事務(wù), 如果外部事務(wù) commit, 潛套事務(wù)也會被 commit, 這個規(guī)則同樣適用于 roll back.

           那么外部事務(wù)如何利用嵌套事務(wù)的 savepoint 特性呢, 我們用代碼來說話

           

          ServiceA {       
              
          /**  
               * 事務(wù)屬性配置為 PROPAGATION_REQUIRED  
               
          */
            
              
          void methodA() {   
                  ServiceB.methodB();    }
             
           }
               
          ServiceB 
          {      
              
          /**  
               * 事務(wù)屬性配置為 PROPAGATION_REQUIRES_NEW  
               
          */
              
              
          void methodB() {   
              }
                   
          }
             

          這種情況下, 因為 ServiceB#methodB 的事務(wù)屬性為 PROPAGATION_REQUIRES_NEW, 所以兩者不會發(fā)生任何關(guān)系, ServiceA#methodA 和 ServiceB#methodB 不會因為對方的執(zhí)行情況而影響事務(wù)的結(jié)果, 因為它們根本就是兩個事務(wù), 在 ServiceB#methodB 執(zhí)行時 ServiceA#methodA 的事務(wù)已經(jīng)掛起了 (關(guān)于事務(wù)掛起的內(nèi)容已經(jīng)超出了本文的討論范圍, 有時間我會再寫一些掛起的文章) . 

          ServiceA {      
              
          /**  
               * 事務(wù)屬性配置為 PROPAGATION_REQUIRED  
               
          */
            
              
          void methodA() {   
                  ServiceB.methodB();   
              }
             
          }
               
          ServiceB 
          {         
              
          /**  
               * 事務(wù)屬性配置為 PROPAGATION_NESTED  
               
          */
              
              
          void methodB() {   
              }
                   
          }
           

          ServiceB#methodB 如果 rollback, 那么內(nèi)部事務(wù)(即 ServiceB#methodB) 將回滾到它執(zhí)行前的 SavePoint(注意, 這是本文中第一次提到它, 潛套事務(wù)中最核心的概念), 而外部事務(wù)(即 ServiceA#methodA) 可以有以下兩種處理方式: 
          1. 改寫 ServiceA 如下 

          ServiceA {      
              
          /**  
               * 事務(wù)屬性配置為 PROPAGATION_REQUIRED  
               
          */
            
              
          void methodA() {   
                  
          try {   
                      ServiceB.methodB();   
                  }
           catch (SomeException) {   
                      
          // 執(zhí)行其他業(yè)務(wù), 如 ServiceC.methodC();   
                  }
             
              }
              
          }
            

          這種方式也是潛套事務(wù)最有價值的地方, 它起到了分支執(zhí)行的效果, 如果 ServiceB.methodB 失敗, 那么執(zhí)行 ServiceC.methodC(), 而 ServiceB.methodB 已經(jīng)回滾到它執(zhí)行之前的 SavePoint, 所以不會產(chǎn)生臟數(shù)據(jù)(相當(dāng)于此方法從未執(zhí)行過), 這種特性可以用在某些特殊的業(yè)務(wù)中, 而 PROPAGATION_REQUIRED 和 PROPAGATION_REQUIRES_NEW 都沒有辦法做到這一點. (題外話 : 看到這種代碼, 似乎似曾相識, 想起了 prototype.js 中的 Try 函數(shù) ) 

          2. 代碼不做任何修改, 那么如果內(nèi)部事務(wù)(即 ServiceB#methodB) rollback, 那么首先 ServiceB.methodB 回滾到它執(zhí)行之前的 SavePoint(在任何情況下都會如此), 外部事務(wù)(即 ServiceA#methodA) 將根據(jù)具體的配置決定自己是 commit 還是 rollback (+MyCheckedException). 
          上面大致講述了潛套事務(wù)的使用場景, 下面我們來看如何在 spring 中使用 PROPAGATION_NESTED, 首先來看 AbstractPlatformTransactionManager 

          JdbcTransactionObjectSupport 告訴我們必須要滿足兩個條件才能 createSavepoint : 
            
          2. java.sql.Savepoint 必須存在, 即 jdk 版本要 1.4+ 
          3. Connection.getMetaData().supportsSavepoints() 必須為 true, 即 jdbc drive 必須支持 JDBC 3.0 


          確保以上條件都滿足后, 你就可以嘗試使用 PROPAGATION_NESTED 了.

          posted @ 2010-11-03 00:11 羔羊| 編輯 收藏

          緩存的基本特性

          緩存基本特性:
          1.時間記錄,進(jìn)入Cache的時間
          2.timeout過期時間,cache里面數(shù)據(jù)多久過期
          3.eviction policy 清楚策略,最不經(jīng)常訪問的數(shù)據(jù),最久沒訪問到的數(shù)據(jù)
          4.命中率:Cache的數(shù)據(jù)被選中的比率
          5.分級Cache,支持 Region分級,比如ehcache
          6.分布式,分布在不同的計算機(jī)上
          7.鎖,事務(wù),數(shù)據(jù)同步

          各個Cluster之間的Cache同步有多種實現(xiàn)方法。比如JMS,RMI 
          Cache中一般有g(shù)et,put,remove,clear
          對于Cluster的Cache來說,讀(get)肯定是local的,只需要從本地內(nèi)存中獲取,而Remove/clear,需要和其他計算機(jī)同步,put可以是local的,因為如果讀不到,可以從數(shù)據(jù)庫中讀
          remote put:一臺計算機(jī)把數(shù)據(jù)放到自己的Cache中,這個數(shù)據(jù)需要傳播到Cluster其他計算機(jī)上,這樣其他Cluster可以同步,但是如果數(shù)據(jù)大,傳播的代價就大了
          local put:只放到本地計算機(jī)的Cache里,不需要同步到其他Cluster,從Cache得不到的數(shù)據(jù)可以在數(shù)據(jù)庫里讀取


          過期數(shù)據(jù):在hibernate等orm工具中,有一個原則,就是不要把沒有commit的數(shù)據(jù)放到緩存中,防止臟讀,而且remove之后必須通知其他Cluster,保證大部分時間內(nèi),給用戶的數(shù)據(jù)不是過期的數(shù)據(jù)
          ORM Cache中,一般分2種Cache,一種是類緩存,一種是查詢緩存,類緩存是以ID對應(yīng)Entity對象,而查詢緩存是用來存放一條查詢語句對應(yīng)的結(jié)果集,然后再到類緩存里找響應(yīng)的實體。
          類緩存:一類Entity一個Region
          查詢緩存:hibernate在一個地方維護(hù)每個表的最后更新時間,其實也就是放在上面org.hibernate.cache. UpdateTimestampsCache所指定的緩存配置里面,當(dāng)通過hibernate更新的時候,hibernate會知道這次更新影響了哪些表。然后它更新這些表的最后更新時間。每個緩存都有一個生成時間和這個緩存所查詢的表,當(dāng)hibernate查詢一個緩存是否存在的時候,如果緩存存在,它還要取出緩存的生成時間和這個緩存所查詢的表,然后去查找這些表的最后更新時間,如果有一個表在生成時間后更新過了,那么這個緩存是無效的。 可以看出,只要更新過一個表,那么凡是涉及到這個表的查詢緩存就失效了,因此查詢緩存的命中率可能會比較低。

          7

          posted @ 2010-11-02 16:30 羔羊| 編輯 收藏

          PriorityBlockingQueue

          PriorityBlockingQueue:一個無界的阻塞隊列,它使用與類 PriorityQueue 相同的順序規(guī)則,并且提供了阻塞檢索的操作。雖然此隊列邏輯上是無界的,但是由于資源被耗盡,所以試圖執(zhí)行添加操作可能會失敗(導(dǎo)致 OutOfMemoryError)。此類不允許使用 null 元素。依賴自然順序的優(yōu)先級隊列也不允許插入不可比較的對象(因為這樣做會拋出 ClassCastException)。

           PriorityBlockingQueue()

                    用默認(rèn)的初始容量 (11) 創(chuàng)建一個 PriorityBlockingQueue,并根據(jù)元素的自然順序排序其元素(使用 Comparable)。 PriorityBlockingQueue(Collection<? extends E> c)
                    創(chuàng)建一個包含指定集合中元素的 PriorityBlockingQueuePriorityBlockingQueue(int initialCapacity)
                    使用指定的初始容量創(chuàng)建一個 PriorityBlockingQueue,并根據(jù)元素的自然順序排序其元素(使用 Comparable)。 PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator)
                    使用指定的初始容量創(chuàng)建一個 PriorityBlockingQueue,并根據(jù)指定的比較器排序其元素。

          此類每次offer元素,都會有一個fixup操作,也就是排序,如果沒有構(gòu)造的時候傳入自己實現(xiàn)的比較器,就采用自然排序,否則采用比較器規(guī)則,進(jìn)行二分查找,比較,保持列頭是比較器希望的那個最大或則最小元素。
           private void fixUp(int k) {
                  if (comparator == null) {
                      while (k > 1) {
                          int j = k >> 1;
                          if (((Comparable<E>)queue[j]).compareTo((E)queue[k]) <= 0)
                              break;
                          Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
                          k = j;
                      }
                  } else {
                      while (k > 1) {
                          int j = k >>> 1;
                          if (comparator.compare((E)queue[j], (E)queue[k]) <= 0)
                              break;
                          Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
                          k = j;
                      }
                  }
              }

          posted @ 2010-11-01 16:33 羔羊 閱讀(885) | 評論 (0)編輯 收藏

          callable

          public interface Callable<V>

          返回結(jié)果并且可能拋出異常的任務(wù)。實現(xiàn)者定義了一個不帶任何參數(shù)的叫做 call 的方法。 此方法返回計算結(jié)果,并拋出經(jīng)過檢查的異常

          與Runnable相似,但是Runnable不返回計算結(jié)果,且不拋異常


          void run()
                    使用實現(xiàn)接口 Runnable 的對象創(chuàng)建一個線程時,啟動該線程將導(dǎo)致在獨立執(zhí)行的線程中調(diào)用對象的 run 方法。


           V call()
                    計算結(jié)果,如果無法計算結(jié)果,則拋出一個異常。



          通過callable 和Runnable構(gòu)建FutureTask任務(wù),調(diào)用run()方法獲得計算結(jié)果,并輸出

          package com.google.minatest.concurrent; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import com.google.minatest.entity.Message; public class FutureTaskStudy { FutureTask<Message> future = null; public static void main(String[] args) { try { new FutureTaskStudy().test(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } public void test() throws InterruptedException, ExecutionException { future = new FutureTask<Message>(new CallableImpl()); future.run(); System.out.println(future.get()); future = new FutureTask<Message>(new RunnableImpl(), null); future.run(); System.out.println(future.get()); } private class CallableImpl implements Callable<Message> { public Message call() throws Exception { return new Message(); } } private class RunnableImpl implements Runnable { public void run() { new Message(); } } }


          posted @ 2010-11-01 15:53 羔羊| 編輯 收藏

          同步包中的定時器

          ScheduledExecutorService 利用線程池進(jìn)行調(diào)度任務(wù),內(nèi)部使用一個DelayedWorkQueue實現(xiàn),返回ScheduledFuture,而DelayQueue是用優(yōu)先級隊列PriorityQueue實現(xiàn)的一個阻塞隊列,優(yōu)先隊列的比較基準(zhǔn)值是時間
            private static class DelayedWorkQueue 
                  extends AbstractCollection<Runnable> 
                  implements BlockingQueue<Runnable> {
                  
                  private final DelayQueue<ScheduledFutureTask> dq = new DelayQueue<ScheduledFutureTask>();
          }

          public class DelayQueue<E extends Delayed> extends AbstractQueue<E>
              implements BlockingQueue<E> {

              private transient final ReentrantLock lock = new ReentrantLock();
              private transient final Condition available = lock.newCondition();
              private final PriorityQueue<E> q = new PriorityQueue<E>();

          下面為一個小例:
          public class ConcurrentTimer {
          public static void main(String[] args) {
          new ConcurrentTimer().getScheduledExecutorService();
          }

          public void getScheduledExecutorService() {
          ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
          service.scheduleAtFixedRate(new Command(), 1, 1, TimeUnit.SECONDS);
          }

          public class Command implements Runnable {

          public void run() {
          System.out.println("Command");
          }

          }
          }

          posted @ 2010-11-01 15:48 羔羊| 編輯 收藏

          volatile

          volatile語義:告訴處理器,不要到工作內(nèi)存中找我,而是直接到主存中操作我,多線程或者多核環(huán)境下,變量共享
          使用volatile要注意,他只能保證可見性,但不能保證原子性;
          如i++之類的操作,他分為read i的值,之后執(zhí)行i+1
          當(dāng)出現(xiàn)并發(fā)情況時,1線程read i的值,而2線程修改了i的值,這個時候1線程如果再將值刷到主存的話就會造成覆蓋。
          可以通過synchronized在同步代碼段,保證原子性
          或者使用jdk1.5的原子包

          posted @ 2010-11-01 14:04 羔羊| 編輯 收藏

          生產(chǎn)者消費者

          package com.google.study.MQ;

          import java.util.LinkedList;
          import java.util.List;
          import java.util.concurrent.locks.Condition;
          import java.util.concurrent.locks.ReentrantLock;

          public class MessageQueue {

          private List<Message> messageList = new LinkedList<Message>();
          private final ReentrantLock lock = new ReentrantLock();
          private Condition notFull = lock.newCondition();
          private Condition notEmpty = lock.newCondition();
          private Integer maxNum = 5;
          private Integer minNum = 0;

          public int size() {
          return messageList.size();
          }

          public void produce(Message e) throws InterruptedException {
          try {
          lock.lock();
          while (messageList.size() == maxNum) {
          notFull.await();
          }
          messageList.add(e);
          notEmpty.signal();
          } finally {
          lock.unlock();
          }
          }

          public void consume() {

          try {
          lock.lock();
          while (messageList.size() == minNum) {
          notEmpty.await();
          }
          messageList.get(0);
          messageList.remove(0);
          notFull.signal();
          } catch (Exception e) {
          e.printStackTrace();
          } finally {
          lock.unlock();
          }
          }

          }
          package com.google.study.MQ;

          public class Consume implements Runnable {
          private MessageQueue queue;

          public Consume(MessageQueue queue) {
          this.queue = queue;
          }

          @Override
          public void run() {
          while (true) {
          queue.consume();
          System.out.println(queue.size());
          try {
          Thread.sleep(50);
          } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }
          }
          }
          }

          package com.google.study.MQ;

          public class Produce implements Runnable {
          private MessageQueue queue;

          public Produce(MessageQueue queue) {
          this.queue = queue;
          }

          @Override
          public void run() {

          while (true) {
          try {
          queue.produce(getMessage());
          Thread.sleep(100);
          } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }
          }
          }

          private Message getMessage() {

          Message m = new Message();
          m.setName("1");
          m.setValue(1);
          return m;
          }

          }
          package com.google.study.MQ;

          import java.io.Serializable;

          public class Message implements Serializable {
          private int value;
          private String name;

          public int getValue() {
          return value;
          }

          public void setValue(int value) {
          this.value = value;
          }

          public String getName() {
          return name;
          }
          package com.google.study.MQ;

          public class Test {
          public static void main(String[] args) {
          MessageQueue queue = new MessageQueue();
          Thread p1 = new Thread(new Produce(queue));
          Thread p2 = new Thread(new Produce(queue));
          Thread p3 = new Thread(new Produce(queue));
          Thread p4 = new Thread(new Produce(queue));

          Thread c1 = new Thread(new Consume(queue));
          Thread c2 = new Thread(new Consume(queue));

          p1.start();
          p2.start();
          p3.start();
          p4.start();
          c1.start();
          c2.start();
          }
          }

          public void setName(String name) {
          this.name = name;
          }

          }

          posted @ 2010-11-01 00:47 羔羊| 編輯 收藏

          ReentrantReadWriteLock讀寫鎖

          public class ReentrantReadWriteLock extends Object implements Object implements ReadWriteLock, Serializable
          1.可重入,可重入的意思當(dāng)前線程已獲該鎖,還可以再獲取,但是讀寫鎖里,WriteLock可以獲取ReadLock,但是ReadLock不能獲取WriteLock
          2.WriteLock可以降級為ReadLock,意思是:先獲取WriteLock,在獲取ReadLock,釋放WriteLock,這時候線程就將keep ReadLock,但是如果ReadLock想要升級為WriteLock,則不可能,因為根據(jù)讀寫鎖的可重入特性,ReadLock排斥所有WriteLock,也就是(1)特性
          3.ReadLock可以被多個線程持有并在作用時排斥任何WriteLock,而WriteLock隔離性比ReadLock高,它是完全的排斥,根據(jù)這一特性,讀寫鎖適合高頻率讀,但不適合高頻率寫
          4.不管是ReadLock,WriteLock都支持Interrupt
          5.WriteLock支持Condition,但是ReadLock不支持Condition,將拋出UnsupportedOperationException
          下面是個小例子:


          package com.google.study;

          import java.util.Map;
          import java.util.concurrent.ConcurrentHashMap;
          import java.util.concurrent.locks.ReentrantReadWriteLock;
          import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
          import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

          public class ReentrantLockStudy {
          private Map<Integer, Result> map = new ConcurrentHashMap<Integer, Result>();
          private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
          private final WriteLock writeLock = lock.writeLock();
          private final ReadLock readLock = lock.readLock();

          public Result get(Integer key) {
          Result value = null;
          readLock.lock();// 獲取讀鎖
          try {
          value = map.get(key);
          if (value == null) {// 如果沒有該結(jié)果
          readLock.unlock();// 必須釋放讀鎖
          writeLock.lock();// 獲取寫鎖
          value = map.get(key);
          if (value == null) {
          value = getResult();
          map.put(key, value);
          }
          readLock.lock();// 重新降級為讀鎖
          writeLock.unlock();
          }
          } catch (Exception e) {
          writeLock.unlock();
          e.printStackTrace();
          } finally {
          readLock.unlock();
          }
          return value;
          }

          public void put(Integer key, Result value) {
          writeLock.lock();
          try {
          map.put(key, value);
          } finally {
          writeLock.unlock();
          }

          }

          private Result getResult() {
          return new Result();
          }

          private static class Result {

          }
          }

          posted @ 2010-10-31 23:31 羔羊| 編輯 收藏

          linux常用命令

          cp:文件復(fù)制
          MV:移動或者重命名
          rm:刪除,rm -rf則是遞歸刪除
          mkdir:創(chuàng)建目錄
          rmdir:刪除空目錄
          cd:改變目錄
          PWD:顯示當(dāng)前的絕對路徑
          ls: 列出子目錄和文件
          chmod:修改權(quán)限 chmod ugo+r file1.txt  u表示用戶,G表示擁有者同組的其他用戶,g表示擁有著不同組的其他用戶,a表示所有
          tar打包 
          打包:tar -cvf filename.tar /home/tt.txt 
               tar -cvf filename.tar /home/t1.txt /home/t2.txt 

          抽取:tar -xvf filename.tar 


          打包并壓縮 
          使用gzip: 
          壓縮tar -czvf filename.tar.gz /home/t1.txt /home/t2.txt 
          解壓tar -xzvf filename.tar.g

          netstat: 
              netstat -pan|grep 2809查看2809端口 
          lsof -i 顯示所有打開的端口 

          posted @ 2010-10-27 13:42 羔羊| 編輯 收藏

          bundle生命周期

          bundle的生命周期分為installed,resovled,starting,active,stopping,unstalled
          install:解析bundle的MANIFEST.MF信息,校驗格式,同時查看是否存在相同的bundle, 分配bundleid,生成bundle對象,并放入已安裝的bundles集合中,狀態(tài)為installed,可以通過命令bundle id來查看這個ID的bundle的關(guān)系圖
          resolve:尋找bundle中所需依賴的包和bundle是否存在以及被resolve,包括import-package,require-bundle,如尋找到則進(jìn)入檢查,檢查完沖突后形成綁定關(guān)系,有個關(guān)系圖直接定位,以便加載類時直接加載。
          start:檢查bundle狀態(tài),如未resolve,則先resolve,尋找MANIFEST.MF中配置的bundle-activator,找到后調(diào)用他的start方法,將bundle狀態(tài)改為 active
          stop:卸載當(dāng)前bundle對外提供的service,并釋放bundle引用的其他服務(wù),之后調(diào)用activator類里的stop方法,改bundle狀態(tài)為resovled
          uninstall:
          檢查bundle狀態(tài),如果為active,則先stop,釋放bundle對其他bundle的類依賴,如其他bundle依賴此bundle的類,則記錄,如沒有,則釋放該bundle的classloader,最終修改bundle的狀態(tài)為unstalled
          update:
          首先是停止當(dāng)前bundle,
          重新安裝并resovle bundle,恢復(fù)到bundle更新之前的狀態(tài)
          如果希望更新bundle所引用到的類,則必須refresh動作,但refresh也值對unreslve狀態(tài)以及uninstall時還有其他類依賴classloader還存活的bundle進(jìn)行unresolve動作,并重新resolve對他們有依賴的bundle,建立新的綁定關(guān)系圖,因此refresh可能會讓某些bundle的classloader重建.

          posted @ 2010-10-27 00:35 羔羊| 編輯 收藏

          OSGI小記

          前幾個人因為項目中考慮使用OSGI來開發(fā),因此和同事調(diào)研了大概1個多月,當(dāng)時沒做記錄,現(xiàn)在來彌補(bǔ)下
          OSIG,一個構(gòu)件模塊化,動態(tài)化系統(tǒng)的標(biāo)準(zhǔn),眾多應(yīng)用服務(wù)器實現(xiàn)微核采用的技術(shù),如weblogic,glassfish,最出名的是equinox,eclipse核心。
          在OSGI環(huán)境中,體現(xiàn)模塊化,動態(tài)化,
          模塊化:模塊之間獨立,部首其他模塊影響。
          其他模塊只能訪問該模塊對外提供的服務(wù)
          模塊具備獨立的生命周期,如啟動,停止,更新
          動態(tài)化:
          對于模塊的增加,修改,刪除,不需要做 額外的處理。
          OSGI中,每個模塊就是在物理上就是一個bundle,一個JAR包,是OSGI部署的最小單位,需要在每個JAR包的MANIFEST.MF中給這個bundle標(biāo)識,象bundle-name,Bundle-SymbolicName,Bundle-ClassPath,Bundle-Activator;
          OSGI中bundle的隔離是通過java ClassLoader隔離性來完成的,JAVA中每個classloader空間都是隔離的,默認(rèn)的classloader有bootStrap ClassLoader(jre/lib,jre/classes) extension classloader(jre/lib/ext),system classloader(-classpath),一般osgi框架還實現(xiàn)自己的應(yīng)用類加載器。
          bundle隔離機(jī)制實現(xiàn):每個bundle都有一個獨立的classloader,通過bundle-classpath指定JAR路徑
          java中通過雙親委托來加載類,尋找類首先是從parent classloader中尋找,如果找不到才在當(dāng)前的clasloader中尋找
          bundle類加載過程:
          java.*開頭的類委派給parent classloader加載;
          bootdelegationcan參數(shù)中配置的也委派給parent classloader加載,parent classloader找不到則繼續(xù)下面的操作;
          是否在import-Package中,在則委派給導(dǎo)出他的bundle的classloader 加載,不在則繼續(xù)下面
          是否在require-bundle中,在則委派給相應(yīng)的bundleloader加載,不在繼續(xù)下面的
          是否在自己的bundle-classpath 中,不在則繼續(xù)下面的
          是否在fragmentsbundle的classpath中尋找,不在則繼續(xù)下面的( 在 OSGi 框架中提供了一種稱為 fragment 的特殊 bundle。在裝載的過程中,這類 fragment 是被附加到被稱為“宿主”的 bundle 上,最后作為一個整體 bundle 運(yùn)行于 OSGi 環(huán)境中。最為典型的 fragment 應(yīng)用場景是多語言包發(fā)布,將包含多語言包的 bundle 作為主體程序 bundle 的 fragment,這樣可以將多語言包和主體 bundle 作為兩個獨立的部分來發(fā)布,但在運(yùn)行時它們又是一個整體。)
          是否在export-package中,不在繼續(xù)下面的
          是否在dynamicimport-package中,在則加載,不在則拋classNotfoundexception
          通過MANIFEST.MF定義import-package等,當(dāng)要注意,最好定義包的版本 如:Import-Package: org.eclipse.osgi.framework.console;version="1.0.0",
           org.osgi.framework;version="1.3.0"
          Import-Package尋找機(jī)制:
          resolved的優(yōu)先未resolved
          版本高的優(yōu)先版本低的,如果版本一樣,則比較bundle id,小的優(yōu)先,也就是先安裝的bundle優(yōu)先



          posted @ 2010-10-26 17:28 羔羊| 編輯 收藏

          cglib入門

          代理為要控制訪問的類提供了一種可行的途徑,他為目標(biāo)類引入一個中間層,JDK1.3后也有動態(tài)代理,不過性能不是很好,1.6有所加強(qiáng)。
          CGLIB是一個強(qiáng)大的代碼生成包,在ASM上建立,在一些開源工具中經(jīng)常可以看到他的身影,比如hibernate,spring。
          CGLIB底層通過字節(jié)碼處理框架ASM來將字節(jié)碼生成新的類,在spring AOP中不強(qiáng)制使用CGLIB,默認(rèn)是JDK動態(tài)代理。
          CGLIB 包情況:
          net.sf.cglib.core:底層字節(jié)碼處理類,大部分與ASM有關(guān)。
          net.sf.cglib.transform:編譯期或運(yùn)行期和類文件的轉(zhuǎn)換
          net.sf.cglib.proxy:實現(xiàn)創(chuàng)建代理和方法攔截器的類
          net.sf.cglib.reflect:實現(xiàn)快速放射
          net.sf.cglib.util:工具包
          net.sf.cglib.beans:javabean相關(guān)工具類
          通過CGLIB創(chuàng)建動態(tài)代理,本質(zhì)上,他是動態(tài)的生成目標(biāo)類的子類,覆蓋目標(biāo)類所有不是final的方法,并給他們設(shè)置好callback,因此,原有類的每個方法調(diào)用就會變成自定義的攔截方法。
          創(chuàng)建動態(tài)代理時通常要用到如下api:net.sf.cglib.proxy.Callback這個接口,他是很關(guān)鍵的一個接口,所有被net.sf.cglib.proxy.Enhancer類調(diào)用的回調(diào)借口都要繼承這個接口
          如:public interface MethodInterceptor
          extends Callback
          {
              /**
               * All generated proxied methods call this method instead of the original method.
               * The original method may either be invoked by normal reflection using the Method object,
               * or by using the MethodProxy (faster).
               * @param obj "this", the enhanced object
               * @param method intercepted Method
               * @param args argument array; primitive types are wrapped
               * @param proxy used to invoke super (non-intercepted method); may be called
               * as many times as needed
               * @throws Throwable any exception may be thrown; if so, super method will not be invoked
               * @return any value compatible with the signature of the proxied method. Method returning void will ignore this value.
               * @see MethodProxy
               */    
              public Object intercept(Object obj, java.lang.reflect.Method method, Object[] args,
                                         MethodProxy proxy) throws Throwable;

          }
          這個是基與方法的回調(diào),第一個參數(shù)是代理對象,第二個是被攔截的方法對象,第三個是方法參數(shù),第四個是方法的代理對象。
          public class MyClass {
          public void method1() {
          System.out.println("method1");
          }

          public void method2() {
          System.out.println("method2");
          }
          }
          攔截器:
          public class MethodInterceptImpl implements MethodInterceptor {

          public Object intercept(Object arg0, Method arg1, Object[] arg2,
          MethodProxy arg3) throws Throwable {
          System.out.println(arg1.getName()+"--intercept");
          arg3.invokeSuper(arg0, arg2);// 這里其實也可以用原來的方法對象來執(zhí)行,但是性能上不如cglib的方法代理類
          return null;
          }
          public class MainTest {
          public static void main(String[] args) {
          simpleTest();
          }

          private static void simpleTest() {
          Enhancer en = new Enhancer();
          en.setCallback(new MethodInterceptImpl());
          en.setSuperclass(MyClass.class);
          MyClass m = (MyClass) en.create();
          m.method1();
          m.method2();
          }
          結(jié)果:method1--intercept
          method1
          method2--intercept
          method2
          現(xiàn)實項目中可能存在某些需求,比如method1需要攔截,而method2不需要攔截。那我們可以對callback做下選擇,使用net.sf.cglib.proxy.CallbackFilter做一些過濾。
          public class MethodFilterImpl implements CallbackFilter {

          private final static int execute = 0;
          private final static int unexecute = 1;

          public int accept(Method method) {
          String methodName = method.getName();
          if("method1".equals(methodName)){
          return execute;
          }
          return unexecute;
          }

          }

          調(diào)用的時候需要給callback設(shè)置好索引位置,因為accept的返回值就是callbacks數(shù)組的索引位置。
          private static void filterTest() {
          Enhancer en = new Enhancer();
          Callback[] callbacks = new Callback[] { new MethodInterceptImpl(),
          NoOp.INSTANCE };//這里攔截器的索引位置要與filter里的設(shè)置一致
          en.setCallbacks(callbacks);
          en.setSuperclass(MyClass.class);
          en.setCallbackFilter(new MethodFilterImpl());
          MyClass m = (MyClass) en.create();
          m.method1();
          m.method2();
          }
          這里callbacks[1]這個位置使用了NoOp這個回調(diào)接口
          public interface NoOp extends Callback
          {
              /**
               * A thread-safe singleton instance of the <code>NoOp</code> callback.
               */
              public static final NoOp INSTANCE = new NoOp() { };
          }
          這個callback其實就是直接把任務(wù)委派給這個方法在父類中的實現(xiàn),其實也等同于沒做什么額外的事情
          執(zhí)行結(jié)果:method1--intercept
          method1
          method2
          method2 并未被MethodInterceptImpl攔截

          }

          posted @ 2010-10-26 14:27 羔羊 閱讀(1042) | 評論 (1)編輯 收藏

          JAVA GC

          JAVA GC有很多種算法,比如引用記數(shù),復(fù)制標(biāo)記-清楚,標(biāo)記-整理,每種算法都有優(yōu)缺點,看特定場景選擇。
          比如當(dāng)很多臨時對象時,復(fù)制算法比較好,因為周期短,復(fù)制的可以少點,如果許多長壽對象,反復(fù)復(fù)制就不樂觀了,這個時候標(biāo)記-整理比較好。
          目前SUN JAVA的GC用的是分代垃圾回收。
          分為年輕代,老年代,持久代
          年輕代存放生命周期很短的對象,這部分對象在GC的時候,很多已經(jīng)是非活動對象,因此采用復(fù)制算法,只需要將少量的存貨對象copy到to space,存貨越少,效率越高。
          年輕代的GC叫minor gc,經(jīng)過多次復(fù)制,依舊存活的對象將移出年輕代,移到年老代。
          年輕代分為:
          eden:每當(dāng)對象創(chuàng)建的時候,總是被分配到這個區(qū)域
          survivor1:復(fù)制算法中的from space
          survivor2:復(fù)制算法中的to space
          年老代:
          生命周期長,經(jīng)過多次minor gc,依舊存活的對象
          老年代的GC 叫major gc,通常也叫full gc
          采用標(biāo)記-整理算法。老年區(qū)域比較大,需要時間長
          minor gc可能引發(fā)full gc。當(dāng)eden+from space空間大于老年代剩余空間時,就會fucc gc,悲觀算法
          持久代:存放class信息和方法信息的地方,可通過-XX:MaxPermSize來調(diào)整最大值
          -XX:NewRatio來設(shè)置年輕代與年老代比值
          -XX:SurvivorRatio設(shè)置eden與survivor區(qū)的比值
          -XX:MaxTenuringThreshold設(shè)置垃圾的年齡,如果=0,則表示不經(jīng)過survivor區(qū),直接進(jìn)入年老代
          -XX:ParallelGCThreads配置并行收集的線程數(shù),最好與CPU數(shù)相同
          -XX:+UseParallelGC 選擇使用并行收集器

          posted @ 2010-10-26 00:56 羔羊| 編輯 收藏

          主站蜘蛛池模板: 客服| 什邡市| 会昌县| 崇仁县| 中西区| 长岭县| 九江市| 乌什县| 文成县| 靖远县| 江北区| 昌乐县| 灵台县| 太湖县| 孝感市| 大荔县| 双江| 洪洞县| 樟树市| 奉化市| 香港 | 隆林| 恩施市| 乐昌市| 罗源县| 河东区| 张掖市| 嘉鱼县| 宁德市| 榆社县| 台湾省| 德江县| 霞浦县| 比如县| 鄄城县| 麻栗坡县| 河北区| 应城市| 锦屏县| 乌海市| 资溪县|