| Home >> All >> com >> neuron >> [ jaffer Javadoc ] |
Source code: com/neuron/jaffer/Queue.java
1 /* 2 * Copyright (c) 2003 Stewart Allen <stewart@neuron.com>. All rights reserved. 3 * This program is free software. See the 'License' file for details. 4 */ 5 6 package com.neuron.jaffer; 7 8 import java.io.*; 9 import java.net.*; 10 import java.util.*; 11 12 public class Queue 13 { 14 private Element head; 15 private Element tail; 16 17 //private int count; 18 //private int maxCount; 19 20 private Element createElement(Object obj) 21 { 22 return new Element(obj); 23 } 24 25 public void enqueue(Object obj) 26 { 27 synchronized (this) 28 { 29 if (tail == null) 30 { 31 tail = createElement(obj); 32 head = tail; 33 } 34 else 35 { 36 tail.next = createElement(obj); 37 tail = tail.next; 38 } 39 /* 40 count++; 41 if (count > maxCount) 42 { 43 maxCount = count; 44 System.out.println("max queue len = "+maxCount+" : "+this); 45 } 46 */ 47 this.notify(); 48 } 49 } 50 51 public Object dequeue() 52 { 53 synchronized (this) 54 { 55 try 56 { 57 while (head == null) 58 { 59 this.wait(); 60 } 61 } 62 catch (InterruptedException ex) 63 { 64 return null; 65 } 66 catch (Exception ex) 67 { 68 ex.printStackTrace(); 69 return null; 70 } 71 Element element = head; 72 if (head.next == null) 73 { 74 head = null; 75 tail = null; 76 } 77 else 78 { 79 head = head.next; 80 } 81 //count--; 82 return element.object; 83 } 84 } 85 86 private class Element 87 { 88 Object object; 89 Element next; 90 91 Element(Object object) 92 { 93 this.object = object; 94 } 95 } 96 } 97