1

I am trying to create a fixed size Queue in java, I only want to store maximum 10 objects in the Queue. However, the queue keeps on storing/adding objects and ignoring the if condition.

my code:

Queue<Customer> sitt = new LinkedList<Customer>();
if(sitt.size() < 10) {
   System.out.println("Added");
   ((LinkedList<Customer>)sitt).offer(cust); 
} else {
   System.out.println("No space..");
}

I have another Runnable class, and I am running 22 threads. This condition should only add 0-9 objects of Customer class. However, the sitt.size() even exceed 20. Can anyone tell me what's the problem here? that even the if condition is being ignored.

P.S: The reason I am using Queue here, is because I have need FIFO.

dazito
  • 7,740
  • 15
  • 75
  • 117
Nix
  • 142
  • 3
  • 13
  • why do you use a LinkedList? a simple Circular buffer would work, too – Domso Oct 20 '16 at 07:44
  • http://stackoverflow.com/questions/9580457/fifo-class-in-java – Stefan Oct 20 '16 at 07:46
  • 1
    Since you are in a multithread scenario, you should put your if condition and adding in a synchronized block. Maybe the other threads check the queue size before the other threads add their elements. – Karura91 Oct 20 '16 at 07:48
  • @Karura91 yes it is within the synchronized block. and the `Added` prints for like 22 times. and `sitt.size()` keeps on increasing rather than stopping it 10. @Domso I will check about Circular buffer. @Stefan I am already using that way of Queue with LinkedList, but it ignores `if` condition. – Nix Oct 20 '16 at 07:53
  • @Nix I can't see anything wrong in this code. Could you post the code relevant to the synchronization too? – Karura91 Oct 20 '16 at 07:57
  • LinkedList isn't thread safe. Why not use a thread safe queue with a fixed size? – Peter Lawrey Oct 20 '16 at 08:55
  • @PeterLawrey can you pleae explain, how linkedlist isn't thread safe? – Nix Oct 20 '16 at 18:24
  • @Nix A data structure has to be specifically implemented to be thread safe or it is not thread safe. LinkedList doesn't attempt to be thread safe. – Peter Lawrey Oct 20 '16 at 19:31

2 Answers2

5

You can just use LinkedBlockingQueue from java.util.concurrent - it allows you to specify the fixed size for the queue.

new LinkedBlockingQueue<>(10);

Then you can insert values into it using offer() method which would do nothing and return false if queue is already full.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
  • 1
    Or ArrayBlockingQueue or ConcurrentLinkedDeque or ConcurrentArrayQueue or ArrayDeque or LinkedBlockingDeque +1 – Peter Lawrey Oct 20 '16 at 08:58
  • Thanks for the suggestions, I am going to try all these. However, LinkedBlockingQueue<>(10); didn't solve the problem. I am going to update my Question with complete codes. – Nix Oct 20 '16 at 18:26
  • @Nix without looking at the code I assume that you're dealing with different instances of queues. That is - you might add item to one queue, but by mistake check the size of another. – Dmitry Zaytsev Oct 21 '16 at 07:51
0

Obviously, issue with such implementation - access to shared resource (queue) from multi threads without any syncronization between them. This leads to so called "race" conditions. In extremum case: all threads reads size, its equal 0. All threads executes condition's body and adds new elements.

I would either use some public solutions for "shared (blocking) circular queue", or implement it via thread-safe (via locking or else) "integer counter in array".

Vaulter
  • 196
  • 2
  • 6