On 03/12/2021 07:23, ???? wrote:
package org.apache.tomcat.util.collections;


public class SynchronizedQueue<T&gt; {
  public static final int DEFAULT_SIZE = 128;
  private Object[] queue;
  private int size;
  private int insert;
  private int remove;


  public SynchronizedQueue() {
    this(128);
  }


  public SynchronizedQueue(int initialSize) {
    this.insert = 0;
    this.remove = 0;
    this.queue = new Object[initialSize];
    this.size = initialSize;
  }


  public synchronized boolean offer(T t) {
    this.queue[this.insert++] = t;
    if (this.insert == this.size) {
      this.insert = 0;
    }


    if (this.insert == this.remove) {
      this.expand();
    }


    return true;
  }

I think that when insert = = remove, the contents of the queue should be empty.

You are wrong. I suggest you use a debugger to create a queue of size 4 and then debug what happens when you do the following:
- call offer() 3 times
- call poll() 2 times
- call offer() 4 times

When multiple pollerevents are produced and not consumed, the capacity needs to 
be expanded

That we agree on.

so I think the code in the red part should exchange positions??

You have misunderstood the code. This is a FIFO queue not a LIFO queue.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to