首先简单概述一下队列:

百度百科对队列的解释:

手写简单的安全队列

 

简单来说就是我们平时的排队取票,谁先排队谁先取票,后排队后取票

 

下面是代码演示:

/**
 * @ClassName
自己实现队列
 
* @Param
 * @retrun
 * @admin
 */

public class Queue<T>{
   
//第一个节点
   
private  Node firstNode;

   
//最后一个节点
   
private  Node lastNode;

   
//队列总长度
   
private int size;

   
//向队列最后添加元素方法
   
public synchronized boolean  put(T val){
       
//队列元素不能为空
       
if (val == null){
           
return  false;
        }
       
final Node n=new Node(val,null);

       
if (size == 0){
           
//表示队列没有数据;
           
//那新增节点就是第一个节点也是最后一个节点
          
firstNode = n;
         
lastNode = n;

        }
else {
           
//如果队列不为空,则将新增节点添加到最后节点的下一个节点,
           
//并且新增节点为最后一个节点
           
lastNode.next = n;
           
lastNode = n;
        }
       
//队列长度加1
       
size++;
       
return true;
    }

   
//获取第一个元素方法
   
public synchronized T get(){
       
//如果队列没有数据,返回null
       
if (size == 0){
           
return  null;
        }
      
final Node n=firstNode;
       
//判断第一个节点的下一个节点存不存在
       
if (size == 1){
           
//表示队列中只有一个节点
            
//取出第一个节点,那队列就没数据
           
firstNode=null;
           
lastNode=null;
        }
else
       
{
           
//表示队列中不止一个节点
           
//取出第一个节点,则下一个节点是第一节点
           
firstNode=n.next;
        }
       
//队列长度减1
       
size--;
       
return n.value;
    }

   
//返回长度
   
public  int getSize(){
       
return  size;
    }

   
//内部类,实现数据链表
   
private    class  Node{
       
public Node(T value, Node next) {
           
this.value = value;
           
this.next = next;
        }

       
public T value;
       
private Node next;
    }


}

 

 

下面是测试代码:


 //true表示线程2正在新增
 
//false表示全部数据新增完成
static boolean b=true;
//Integet队列
static Queue<Integer> queue=new Queue<Integer>();
 
@Test
 
void  contextLoads() throws IOException {

    
//开启新线程
    
new Thread(new Runnable() {
        
@Override
        
public void run() {

           
Integer i= queue.get();
            
//如果线程2还在新增数据或者队列还有数据,进入循环
           
while (b || i != null){

              
if (i == null){
                  
try {
                      
//如果没获取到数据,则线程等待0.5
                      
Thread.sleep(500);
                   }
catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
else {
                  
//获取到数据则输出
                  
System.out.println(i);
               }
              
//重新获取队列数据
               
i= queue.get();
            }

         }
     }).start();

    
new Thread(new Runnable() {
        
@Override
        
public void run() {
            
//循环添加100个元素
             
for (int i = 0; i < 100; i++){
                
queue.put(i);
             }
            
//添加完成将表示改为false;
            
b=false;
         }
     }).start();

    
System.in.read();
 }

 

测试结果:

手写简单的安全队列

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
猜你喜欢
  • 2021-06-19
  • 2022-12-23
  • 2021-07-05
  • 2021-07-14
  • 2021-10-24
  • 2022-12-23
  • 2021-06-01
相关资源
相似解决方案