1 21 22 package org.webdocwf.util.loader; 23 24 import java.util.*; 25 26 33 public class BufferOctopusClass { 34 35 static private BufferOctopusClass instance; 36 private ArrayList buffer; 37 private static boolean isUsed=false; 38 39 44 static public BufferOctopusClass getInstance() { 45 if (instance == null) { 46 instance = new BufferOctopusClass(); 47 } 48 return instance; 49 } 50 51 54 private BufferOctopusClass() { 55 init(); 56 } 57 58 61 private void init(){ 62 buffer=new ArrayList(); 63 } 64 65 69 public synchronized void writeToBuffer(String msg){ 70 if(isUsed==true){ 71 buffer.add(0,msg); 72 notify(); 73 } 74 } 75 76 80 public synchronized String readFromBuffer(){ 81 if(buffer.isEmpty()){ 82 try{ 83 wait(); 84 }catch(Exception ex){ 85 } 87 } 88 String ret=""; 89 if(buffer.size()>0) 90 ret=(String ) buffer.remove(buffer.size()-1); 91 notify(); 92 if( ret == null ) 93 ret = ""; 94 return ret; 95 } 96 97 101 public boolean IsUsed(){ 102 return isUsed; 103 } 104 105 108 public void empty () { 109 buffer.clear(); 110 } 111 112 115 public void setUsed(){ 116 isUsed=true; 117 } 118 } | Popular Tags |