zoukankan      html  css  js  c++  java
  • 实现一个线程安全的Queue队列

    使用装饰者模式实现一个线程安全的Queue队列。

    public class SynchronizedQueue<E> implements Queue<E>, Serializable {

    private static final long serialVersionUID = -1439242290701194806L;

    private final Queue<E> q;

    public SynchronizedQueue(Queue<E> q) {
    this.q = q;
    }

    public synchronized boolean add(E e) {
    return q.add(e);
    }

    public synchronized E element() {
    return q.element();
    }

    public synchronized boolean offer(E e) {
    return q.offer(e);
    }

    public synchronized E peek() {
    return q.peek();
    }

    public synchronized E poll() {
    return q.poll();
    }

    public synchronized E remove() {
    return q.remove();
    }

    public synchronized boolean addAll(Collection<? extends E> c) {
    return q.addAll(c);
    }

    public synchronized void clear() {
    q.clear();
    }

    public synchronized boolean contains(Object o) {
    return q.contains(o);
    }

    public synchronized boolean containsAll(Collection<?> c) {
    return q.containsAll(c);
    }

    public synchronized boolean isEmpty() {
    return q.isEmpty();
    }

    public synchronized Iterator<E> iterator() {
    return q.iterator();
    }

    public synchronized boolean remove(Object o) {
    return q.remove(o);
    }

    public synchronized boolean removeAll(Collection<?> c) {
    return q.removeAll(c);
    }

    public synchronized boolean retainAll(Collection<?> c) {
    return q.retainAll(c);
    }

    public synchronized int size() {
    return q.size();
    }

    public synchronized Object[] toArray() {
    return q.toArray();
    }

    public synchronized <T> T[] toArray(T[] a) {
    return q.toArray(a);
    }

    @Override
    public synchronized boolean equals(Object obj) {
    return q.equals(obj);
    }

    @Override
    public synchronized int hashCode() {
    return q.hashCode();
    }

    @Override
    public synchronized String toString() {
    return q.toString();
    }
    }

  • 相关阅读:
    JSP九大隐含对象
    男人失业了,他没有告诉女人
    Ajax与C#应用详细实例
    成都软件园实习那些话
    C#的线程池的那些事
    一家公司的面试经历和现在的我
    我经常逛的几个网站
    我们该如何看待程序员的加班生活?
    socket心跳+聊天
    Python pip 更新问题汇总
  • 原文地址:https://www.cnblogs.com/quyongjin/p/3296616.html
Copyright © 2011-2022 走看看