java代码实现队列的优化

package com.voole.queun;
/**
 * @Decription 队列
 * @author TMAC-J
 *
 */
public class Queun {
    /**
     * 初始化队列尺寸
     */
    private int queunSize = 0;
    /**
     * 初始化头指针 
     */
    private int front = -1;
    /**
     * 初始化尾指针
     */
    private int rear = 0;
    /**
     * 声明数组
     */
    private int[] array;
    /**
     * 当前大小
     */
    private int curentSize = 0;
    /**
     * 构造方法
     * @param queunSize
     */
    public Queun(int queunSize){
        this.queunSize = queunSize;
        array = new int[this.queunSize];
    }
    /**
     * 读操作
     */
    public synchronized void read(){
        if(!isEmpty()){
            front = (front+1)%queunSize;
       array[front] = null; curentSize
--; } else{ System.out.println("当前队列为空!"); /** * 优化CPU时间片的利用率,若当前队列为空会切换到另外的线程,不会继续执行此线程浪费时间和空间 */ try { this.notifyAll();//唤醒其他所有线程 this.wait();//释放对象锁,将当前线程置为阻塞 this.notify();//唤醒当前线程 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("front"+front); } /** * 写操作 * @param data */ public synchronized void write(int data){ if(!isFull()){ array[rear] = data; rear = (rear+1)%queunSize; curentSize++; } else{ System.out.println("当前队列已满"); try { this.notifyAll(); this.wait(); this.notify(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("rear"+rear); } /** * 判断是否是满 */ public boolean isFull(){ if(curentSize == queunSize){ return true; } else{ return false; } } /** * 判断是否是空 */ public boolean isEmpty(){ if(curentSize == 0){ return true; } else{ return false; } }
  

   public Object getQueunHead(){
      return array[(front+1)%queunSize];
      }


}
package com.voole.queun;

public class Test {
    
    public static void main(String[] args) {
        Queun queun = new Queun(10);
        Thread writeThread = new Thread(new WriteThread(queun));
        writeThread.start();
        Thread readThread = new Thread(new ReadThread(queun));
        readThread.start();
    }
    
    private static class ReadThread implements Runnable{
        private Queun queun;
        public ReadThread(Queun queun){
            this.queun = queun;
            
        }
        @Override
        public void run() {
            int i = 100;
            if(queun!=null){
                while(i>0){
                    queun.read();
//                    System.out.println("read"+i);
                    i--;
                }
            }
        }
    }
    
    private static class WriteThread implements Runnable{
        private Queun queun;
        public WriteThread(Queun queun){
            this.queun = queun;
        }
        @Override
        public void run() {
            int i = 100;
            if(queun!=null){
                while(i>0){
                    queun.write(i);
//                    System.out.println("write"+i);
                    i--;
                }
            }
        }
    }
}

现在在实习,每天也就改改bug,利用闲暇时间,研究一下数据结构,收货还是蛮大的,这是队列的优化java代码实现方式。如果还有什么想要了解的,可以参考一下http://blog.csdn.net/sinat_33713995/article/details/51331314和https://zhidao.baidu.com/question/1947170630893457148.html

这两篇解释的比较详细

转载于:https://www.cnblogs.com/yzjT-mac/p/6054973.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值