Results 1 to 4 of 4
  1. #1
    Micky is offline Member
    Join Date
    Oct 2016
    Posts
    4
    Rep Power
    0

    Lightbulb How can I get Fixed size Queue

    Hi there,

    I am trying to create a Queue, and I want to insert data in it only if it's size is less than 10. But however, on inserting it is even inserting in the size if its size is already 10. looks like IF condition is not working.

    Java Code:
    	Queue<Customer> sitt = new LinkedList<Customer>();
            if(sitt.size() < 10) {
    		System.out.println("Customer #" + cust.iD + " is sitting...");
    		((LinkedList<Customer>)sitt).offer(cust); 
    	}
    If I run a thread, and add like 22 objects, they all will be added, however my condition is " < 10".
    Any solution for this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How can I get Fixed size Queue

    I want to insert data
    Where is the code that does the add to insert data?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: How can I get Fixed size Queue

    If you implement a Queue as LinkedList you can not define a maximum number of elements. If you are looking for a queue of limited size you have to look into other types like ArrayBlockingQueue.

    As Norm said, your presented snippet does not provide enough info for us to see why the if statement does not give you the expected result.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How can I get Fixed size Queue

    And your example does not make sense. Each time that code is run you create a new Queue. The size is 0 so you add (via the offer)
    a new element.

    And you don't need to cast Queue to use Queue.offer() since it is defined in the Queue interface.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 9
    Last Post: 04-03-2014, 07:35 PM
  2. Replies: 20
    Last Post: 01-03-2012, 05:15 PM
  3. Replies: 0
    Last Post: 05-27-2011, 08:46 AM
  4. Replies: 0
    Last Post: 03-01-2011, 07:57 AM
  5. How to correctly use a fixed size thread pool?
    By johann_p in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-19-2010, 02:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •