'Collection Buffer'에 해당되는 글 1건

  1. 2010.02.19 Collection Buffer
반응형
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.BlockingBuffer;
import org.apache.commons.collections.buffer.PriorityBuffer;

public class BufferExample {
  public static void main(String args[]) {
    Buffer buffer = new PriorityBuffer();

    buffer.add("2");
    buffer.add("1");

    buffer = BlockingBuffer.decorate(buffer);

    buffer.remove();

    System.err.println(buffer);
    buffer.clear();

    AddElementThread runner = new AddElementThread(buffer);
    runner.start();

    buffer.remove();
    System.err.println(buffer);
  }
}

class AddElementThread extends Thread {
  private Buffer buffer;

  public AddElementThread(Buffer buffer) {

    this.buffer = buffer;
  }

  public void run() {
    try {
      sleep(2000);
    } catch (InterruptedException ie) {}

    buffer.add("3");
  }
}

Posted by 1010