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

这两篇解释的比较详细

java代码实现队列的优化的更多相关文章

  1. Java数组模拟队列 + 优化

    队列介绍 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则. 即:先存入队列的数据,要先取出.后存入的要后取出 示意图:(使用数组模拟队列示意图)  数组模拟队列 队列本身是有序列表 ...

  2. [大牛翻译系列]Hadoop(15)MapReduce 性能调优:优化MapReduce的用户JAVA代码

    6.4.5 优化MapReduce用户JAVA代码 MapReduce执行代码的方式和普通JAVA应用不同.这是由于MapReduce框架为了能够高效地处理海量数据,需要成百万次调用map和reduc ...

  3. Linkedin工程师是如何优化他们的Java代码的(转)

    英文原文:LinkedIn Feed: Faster with Less JVM Garbage 最近在刷各大公司的技术博客的时候,我在Linkedin的技术博客上面发现了一篇很不错博文.这篇博文介绍 ...

  4. java代码之美(11)---java代码的优化

    java代码的优化 随着自己做开发时间的增长,越来越理解雷布斯说的: 敲代码要像写诗一样美.也能理解有一次面试官问我你对代码有洁癖吗? 一段好的代码会让人看就像诗一样,也像一个干净房间会让人看去很舒服 ...

  5. Linkedin工程师是如何优化他们的Java代码的

    http://greenrobot.me/devpost/java-faster-less-jvm-garbage/ Linkedin工程师是如何优化他们的Java代码的 最近在刷各大公司的技术博客的 ...

  6. 如何优化JAVA代码

    通过使用一些辅助性工具来找到程序中的瓶颈,然后就可以对瓶颈部分的代码进行优化.一般有两种方案:即优化代码或更改设计方法.我们一般会选择后者,因为不去调用以下代码要比调用一些优化的代码更能提高程序的性能 ...

  7. Java 性能优化手册 — 提高 Java 代码性能的各种技巧

    转载: Java 性能优化手册 - 提高 Java 代码性能的各种技巧 Java 6,7,8 中的 String.intern - 字符串池 这篇文章将要讨论 Java 6 中是如何实现 String ...

  8. 初试kafka消息队列中间件二(采用java代码收发消息)

    初试kafka消息队列中间件二(采用java代码收发消息) 上一篇 初试kafka消息队列中间件一 今天的案例主要是将采用命令行收发信息改成使用java代码实现,根据上一篇的接着写: 先启动Zooke ...

  9. java代码(11) ---java代码的优化

    java代码的优化 参考了一些Java开发手册有关代码的规范,觉得一段好的代码可以从三个维度去分析.1)性能,2)可扩展性,3)可读性 让我们看看别人是怎么去分析,还有值得我们去学习的地方,也是我正在 ...

随机推荐

  1. ls /usr/linkapp 没反应

    ls /usr/linkapp ll /usr/linkapp  都是一样无反应 没有任何反应, ctrl + c /  ctrl + d 都不行 但是 ls /usr/linkapp/ | wc - ...

  2. Hadoop HDFS 用户指南

    This document is a starting point for users working with Hadoop Distributed File System (HDFS) eithe ...

  3. 基于Spring Mvc实现的Excel文件上传下载

    最近工作遇到一个需求,需要下载excel模板,编辑后上传解析存储到数据库.因此为了更好的理解公司框架,我就自己先用spring mvc实现了一个样例. 基础框架 之前曾经介绍过一个最简单的spring ...

  4. Atitit 深入理解耦合Coupling的原理与attilax总结

    Atitit 深入理解耦合Coupling的原理与attilax总结     耦合是指两个或两个以上的电路元件或电网络等的输入与输出之间存在紧密配合与相互影响,并通过相互作用从一侧向另一侧传输能量的现 ...

  5. API调试工具推荐 - httpie

    API调试工具推荐 - httpie <HelloGitHub>第07期上面看到这个python项目,好东西 文档地址 但是安装的时候报错,google之后发现是个已知的bug,直接使用p ...

  6. VB中 '&' 和 '+' 号的区别

    释义 &(Ampersand)是英语单字and之代表符号,亦可用作中文中的“和”.“与”之代表符号.这个符号源于拉丁文的et的连写. 可读做 ampersand,即 "and per ...

  7. WaitType:ASYNC_IO_COMPLETION

    项目组有一个数据库备份的Job运行异常,该Job将备份数据存储到remote server上,平时5个小时就能完成的备份操作,现在运行19个小时还没有完成,backup命令的Wait type是 AS ...

  8. 对Big Table进行全表更新,导致 Replication 同步数据的过程十分缓慢

    在Publisher database中更新一个big table,数据行数是3.4亿多.由于没有更新 clustered Index key,因此,只产生了3.4亿多个Update Commands ...

  9. SSIS 数据源组件的External Metadata和Advanced Property

    1,SSIS的组件属性ValidateExternalMetadata 如果一个Destination组件使用的是上游创建的staging table,那么必须设置 ValidateExternalM ...

  10. ExtJS面向对象

    序言 1.ExtJs是一套很好的后台框架.现在很流行的,我们要会. 2.这是我写ExtJs的第一篇,以后会写很多直到把这框架运用的炉火纯青,走火入魔. ExtJs中的命名空间 我是做.net的,这命名 ...