自定义队列实现

package async;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 自定义队列
 *
 * @version 2013-9-9
 */
public class Queue<T>
{
    /**
     * 默认队列容量
     */
    private static final int DEFAULT_SIZE = 100;

    /**
     * 队列容量
     */
    private int size;

    /**
     * 队列数据
     */
    private List<T> data = new ArrayList<T>();

    /**
     * 读取锁
     */
    private Object lock = new Object();

    /**
     *
     * 构造函数
     */
    public Queue()
    {
        this.size = DEFAULT_SIZE;
    }

    /**
     *
     * 构造函数
     *
     * @param size
     *            队列容量
     */
    public Queue(int size)
    {
        this.size = size;
    }

    /**
     *
     * 添加任务
     *
     * @param t
     *            任务对象
     * @param isWait
     *            队列满是否等待
     * @return 是否添加成功
     */
    public boolean put(T t, boolean isWait)
    {
        synchronized (lock)
        {
            if (isFull())
            {
                if (isWait)
                {
                    try
                    {
                        lock.wait();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                        return false;
                    }
                }
                else
                {
                    System.out.println("The queue is full;");
                    return false;
                }
            }
            data.add(t);
            lock.notifyAll();
            return true;
        }
    }

    /**
     *
     * 获取任务
     *
     * @param isWait
     *            队列空是否等待
     * @return 任务对象
     */
    public T get(boolean isWait)
    {
        synchronized (lock)
        {
            if (isEmpty())
            {
                if (isWait)
                {
                    try
                    {
                        lock.wait();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                        return null;
                    }
                }
                else
                {
                    System.out.println("The queue is empty;");
                    return null;
                }
            }
            T task = data.remove(0);
            lock.notifyAll();
            return task;
        }
    }

    /**
     *
     * 队列是否为空
     *
     * @return 队列是否为空
     */
    public synchronized boolean isEmpty()
    {
        return 0 == data.size();
    }

    /**
     *
     * 队列是否已满
     *
     * @author Jinglong Wu
     * @return 队列是否已满
     */
    public synchronized boolean isFull()
    {
        return this.size == data.size();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值