java类声明

时间:2012-03-14 17:10:53

标签: java

  

可能重复:
  java class declaration <T>

有时我看到java类创建如下,

  public abstract class ObjectPool<T> {
  private long expirationTime;

  private Hashtable<T, Long> locked, unlocked;

  public ObjectPool() {
    expirationTime = 30000; // 30 seconds
    locked = new Hashtable<T, Long>();
    unlocked = new Hashtable<T, Long>();
  }

  protected abstract T create();

  public abstract boolean validate(T o);

  public abstract void expire(T o);

  public synchronized T checkOut() {
    long now = System.currentTimeMillis();
    T t;
    if (unlocked.size() > 0) {
      Enumeration<T> e = unlocked.keys();
      while (e.hasMoreElements()) {
        t = e.nextElement();
        if ((now - unlocked.get(t)) > expirationTime) {
          // object has expired
          unlocked.remove(t);
          expire(t);
          t = null;
        } else {
          if (validate(t)) {
            unlocked.remove(t);
            locked.put(t, now);
            return (t);
          } else {
            // object failed validation
            unlocked.remove(t);
            expire(t);
            t = null;
          }
        }
      }
    }
    // no objects available, create a new one
    t = create();
    locked.put(t, now);
    return (t);
  }

  public synchronized void checkIn(T t) {
    locked.remove(t);
    unlocked.put(t, System.currentTimeMillis());
  }
}

full code

这里,<T>的含义是什么?它的目的是什么?请解释一下。

3 个答案:

答案 0 :(得分:2)

这是Java中的Generic。见http://en.wikipedia.org/wiki/Generics_in_Java

答案 1 :(得分:1)

阅读您所链接的内容。

它允许您使用特定类型扩展类。

示例:

public class JDBCConnectionPool extends ObjectPool<Connection> { .... }

答案 2 :(得分:1)

它是名为Generics

的功能的一部分