Skip to main content
retag, formatting
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157
public class BQueue<T> {

    private Queue<T> q = new LinkedList<T>();
    private int limit;

    public BQueue(int limit) {
        this.limit = limit;
    }

    public synchronized void put (T t) throws InterruptedException {
        while (isFull()) {
            wait();
        }
        boolean e = isEmpty();
        q.add(t);
        if (e)
            notifyAll();
    }


    public synchronized T get () throws InterruptedException {
        while (isEmpty()) {
            wait();
        }
        boolean f = isFull();
        T t = q.poll();
        if (f)
            notifyAll();
        return t;
    }

    private boolean isEmpty() {
        return q.size() == 0;
    }
    private boolean isFull() {
        return q.size() == limit;
    }
}

Is this implementation thread-safe  ?

public class BQueue<T> {

    private Queue<T> q = new LinkedList<T>();
    private int limit;

    public BQueue(int limit) {
        this.limit = limit;
    }

    public synchronized void put (T t) throws InterruptedException {
        while (isFull()) {
            wait();
        }
        boolean e = isEmpty();
        q.add(t);
        if (e)
            notifyAll();
    }


    public synchronized T get () throws InterruptedException {
        while (isEmpty()) {
            wait();
        }
        boolean f = isFull();
        T t = q.poll();
        if (f)
            notifyAll();
        return t;
    }

    private boolean isEmpty() {
        return q.size() == 0;
    }
    private boolean isFull() {
        return q.size() == limit;
    }
}

Is this implementation thread-safe  ?

public class BQueue<T> {

    private Queue<T> q = new LinkedList<T>();
    private int limit;

    public BQueue(int limit) {
        this.limit = limit;
    }

    public synchronized void put (T t) throws InterruptedException {
        while (isFull()) {
            wait();
        }
        boolean e = isEmpty();
        q.add(t);
        if (e)
            notifyAll();
    }


    public synchronized T get () throws InterruptedException {
        while (isEmpty()) {
            wait();
        }
        boolean f = isFull();
        T t = q.poll();
        if (f)
            notifyAll();
        return t;
    }

    private boolean isEmpty() {
        return q.size() == 0;
    }
    private boolean isFull() {
        return q.size() == limit;
    }
}

Is this implementation thread-safe?

Tweeted twitter.com/#!/StackCodeReview/status/149133251167010816
code format
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157
public class BQueue<T> {

    private Queue<T> q = new LinkedList<T>();
    private int limit;

    public BQueue(int limit) {
        this.limit = limit;
    }

    public synchronized void put (T t) throws InterruptedException {
        while (isFull()) {
            wait();
        }
        boolean e = isEmpty();
        q.add(t);
        if (e)
            notifyAll();
    }


    public synchronized T get () throws InterruptedException {
        while (isEmpty()) {
            wait();
        }
        boolean f = isFull();
        T t = q.poll();
        if (f)
            notifyAll();
        return t;
    }

    private boolean isEmpty() {
        return q.size() == 0;
    }
    private boolean isFull() {
        return q.size() == limit;
    }
}

}

Is this implementation thread-safe ?

public class BQueue<T> {

private Queue<T> q = new LinkedList<T>();
private int limit;

public BQueue(int limit) {
    this.limit = limit;
}

public synchronized void put (T t) throws InterruptedException {
    while (isFull()) {
        wait();
    }
    boolean e = isEmpty();
    q.add(t);
    if (e)
        notifyAll();
}


public synchronized T get () throws InterruptedException {
    while (isEmpty()) {
        wait();
    }
    boolean f = isFull();
    T t = q.poll();
    if (f)
        notifyAll();
    return t;
}

private boolean isEmpty() {
    return q.size() == 0;
}
private boolean isFull() {
    return q.size() == limit;
}

}

Is this implementation thread-safe ?

public class BQueue<T> {

    private Queue<T> q = new LinkedList<T>();
    private int limit;

    public BQueue(int limit) {
        this.limit = limit;
    }

    public synchronized void put (T t) throws InterruptedException {
        while (isFull()) {
            wait();
        }
        boolean e = isEmpty();
        q.add(t);
        if (e)
            notifyAll();
    }


    public synchronized T get () throws InterruptedException {
        while (isEmpty()) {
            wait();
        }
        boolean f = isFull();
        T t = q.poll();
        if (f)
            notifyAll();
        return t;
    }

    private boolean isEmpty() {
        return q.size() == 0;
    }
    private boolean isFull() {
        return q.size() == limit;
    }
}

Is this implementation thread-safe ?

Source Link
a3user
  • 151
  • 1
  • 1
  • 3

Java blocking queue

public class BQueue<T> {

private Queue<T> q = new LinkedList<T>();
private int limit;

public BQueue(int limit) {
    this.limit = limit;
}

public synchronized void put (T t) throws InterruptedException {
    while (isFull()) {
        wait();
    }
    boolean e = isEmpty();
    q.add(t);
    if (e)
        notifyAll();
}


public synchronized T get () throws InterruptedException {
    while (isEmpty()) {
        wait();
    }
    boolean f = isFull();
    T t = q.poll();
    if (f)
        notifyAll();
    return t;
}

private boolean isEmpty() {
    return q.size() == 0;
}
private boolean isFull() {
    return q.size() == limit;
}

}

Is this implementation thread-safe ?