Capacity bound queue: discard the oldest element

Hi,
After my program failed with an OutOfMemoryError, I started to think that binding the capacity of a LinkedBlockingQueue would be a necessary change. Now if the maximum capacity is reached then offer() will fail and return false.
So when the queue is full, then the most recent element will be lost (i.e. not added). This is acceptable, but it would be better if the oldest element were lost instead.
I was thinking about something like this:
boolean added = queue.offer(myElement);
if (!added) {
    queue.poll();
    queue.offer(myElement);
}There's a problem with the above code: it's not atomic. Another thread could fill the queue again after the old element was discarded and before the new one was added. Synchronizing may help but it would also be a performance decrease in my case.
Can someone suggest a better approach?
Thanks

irey wrote:
Synchronizing may help but it would also be a performance decrease in my case.Really? Have you rigorously tested for the performance decrease and been clearly shown it was an issue you actually need to worry about?

Similar Messages

  • How to insert element in a Queue as the nth item

    Hi Gurus,
    Please can anyone help with a method or algorithm that inserts item into the queue as the nth element (counting from front, which is element 1)?
    I have already spent a week on it...i can't seem to knock it out.
    Thanks in advance.

    If the array has enough space
    void insertIntoArray(int n, int value){
         int temp, temp2;
         temp = arrayQueue[n];
         arrayQueue[n] = value;
         for(; n < arrayQueue.lenght; n++){
              temp2 = arrayQueue[n+1];
              arrayQueue[n] = temp;
              temp = temp2;
    }If not
    void insertIntoArray(int n, int value){
         int[] arrayQueue2 = new int[arrayQueue.length];
         for(int i = 0; i < n; i++){
              arrayQueue2[i] = arrayQueue;
         arrayQueue2[n] = value;
         for(++n; n < arrayQueue2.length; n++){
              arrayQueue2[n] = arrayQueue[n+1];
    arrayQueue = arrayQueue2;

  • Is it possible to enqueue multiple elements in a queue at the same time

    I want to know that is it possible to add mutiple elements in a queue at the same time, i am able to do one element at a time if i want iw ill be using for loop, but is it possible without for loop????
    nilesh

    You can't, but if you may redefine the  queue datatype as an array.
    Paolo
    LV 7.0, 7.1, 8.0.1, 2011

  • Changing ThreadPoolExecutor to prefer thread use to bounded queue

    I would like to extend ThreadPoolExecutor to create new threads up to maximumPoolSize and then put tasks onto a bounded queue. (Kind of a cross between Fixed and Cached thread pools).
    It seems that the only place to do this would be to rewrite the execute method of ThreadPoolExecutor and swap the following commands
    if (workQueue.offer(command))  // Swap this
      return;
    Runnable r = addIfUnderMaximumPoolSize(command);  // with this
    if (r == command)
      return;
    if (r == null)
      reject(command).
    ...However with most methods and vars private in ThreadPoolExecutor, overriding this method is nearly impossible.
    Is there a better way of doing this?
    Thanks,
    Chris

    nope - there is no chance. sadly not! otherwise i would have integrated it into my templates
    btw - you could change all the links into a rollover, but that would mean that ALL links are a rollover.
    another way is to use iwebmore to edit the template files: http://iwebmore.ctrl-j.eu/iWebMore.html
    max karreth

  • The UI element of the view will be desplaying in desable mode

    Hi,
    I have developed an application which is in the SAP Help Creating an Email Client Using Web Dynpro and Web Services.
    Followed as it is steps in that example.
    it has deployed without any errors,
    but when we run the application its displaying all the UI Elements in disable mode.
    could any one help on this,
    Thanks & Regards,
    Ravinder Jilla.

    Hi
    In supply function you are itself creating an element of node. no need to create it again.
    By writing only following code you can create a node element which can enable all UI elements.
    // In Supply function
    node.addElement(node.create<your node>Element());
    Other wise you can also do this in wdDoInit() of view controller
    IPrivateView<your View>.I<your node>Element() element = wdContext.create<your node> element();
    wdContext.node<your node>().addElement ( element );
    this will create an element of your node and enable UI element bound to it.
    Try the latter one and let me know the status
    Mandeep Virk

  • Is it a queue managing the BULK jobs?

    Hi all,
    When creating several jobs to import several batches data to Eloqua by Bulk APIs, would the jobs run one by one? Are the execute by the creating order?
    Thanks,
    Biao

    Hi Biao,
    Based on my own observation, jobs are processed one by one, and the order is generally the order in which they were added to the queue. A data transfer service runs first to parse the amount of rows on the file, and then puts it back into the queue again for loading into the db. You might see this in your sync logs as .json files being converted to .csv once you trigger the sync.
    Depending on the amount of data present after parsing, it can affect what order it is processed in due to some advanced load balancing logic in place. For example, if you push one really big file and a few smaller files, chances are, the system will more frequently have small chunks of time/resources free to process a smaller file. The worker looks at the oldest file in the queue, sees that it is 'too big' to handle in the time it has, skips it and then grabs the next file which may be smaller. In most scenarios however, you don't see the system skip over large files that often or for very long. The net effect of this behaviour is that if you load a massive file into the system that it can't process right away, it's not going to cause a few minutes of unnecesssary blockage for other smaller files it can manage right away.
    Again, this is based on observation and such backend logic is subject to change/tweaking at any time.
    Regards,
    Bojan

  • Overwriting the oldest data in the file

    Hi,
      I have a file of 10MB want to write in to file continuously overwriting the oldest data, my file size should be maintained 10MB and program may run for quite long time. since i am new to labview used file operations, shiftregisters and little queues but i am not getting any satisfactory results, hopping my problem is simple and someone could help me on this.
    Thanks,
    ranjan.

    Well, we earlier solved it for the 20MB case, so the 10MB case should be trivial to solve.
    What is the structure of the file, are all records of equal lenght?
    Yes, most likely the solution is simple! Maybe you can show us your code that produces unsatisfactory results and tell us what kind of results would be satisfactory.
    LabVIEW Champion . Do more with less code and in less time .

  • Deadlock with blocking bounded queues

    Say, a class (with plenty of instances) has two blocking bounded queue - Q1 and Q2, and methods
    m1 : takes from Q1
    m2 : puts to Q2
    m3 : puts to Q1
    m4 : takes from Q2
    m3 and m4 are executed independantly in fixed threads pool.
    m1 and m2 are executed in the same thread for an instance. If this is a new thread for each class instance, all works ok, buf if I use fixed threads pool also, I get deadlock.
    Can not realise deadlock reason. Any help?

    anli wrote:
    Say, a class (with plenty of instances) has two blocking bounded queue - Q1 and Q2, and methods
    m1 : takes from Q1
    m2 : puts to Q2
    m3 : puts to Q1
    m4 : takes from Q2
    m3 and m4 are executed independantly in fixed threads pool.
    m1 and m2 are executed in the same thread for an instance. If this is a new thread for each class instance, all works ok, buf if I use fixed threads pool also, I get deadlock.
    Can not realise deadlock reason. Any help?Not enough threads in the thread pool? Can you post a small self-contained example?

  • All my desktop files will now only open in the photoshop elements edit page. How can this be corrected?

    As stated in my question I tried to open a manual I have on my desktop and it opened it Photoshop Elements edit page in a thumb nail format.  I checked and all my desktop items will only open in the photoshop elements edit page.  what do I need to do to correct this?

    Now that we have your post in the correct forum, I have a few questions, please:
    What is the file format of this "manual?"
    What OS version are you running?
    If this is Windows (a PC), then what is that file format associated with in File Types? It is likely that the particular format has been associated with PsE. If that is not what you want, the association can be changed from the Control Panel, to link it to the desired program.
    Good luck,
    Hunt

  • IPhone downloads the *oldest* 50 mails

    Hi,
    I created a new mail account and moved all e-mails from my old account to the new one (so to have one inbox where to received mail and from where to look up old e-mails in case I need to).
    I then created a new inbox on the iPhone (3G v 3.0). When going online with the new inbox on the iPhone, it only downloads the oldest 50 e-mails. I guess I could "download more", 50 at a time, but since there are thousands of messages I don't want to do this, but I'd still like the latest 50 rather than oldest 50 messages.
    Does anyone know how to set up iPhone so that it downloads the latest 50 messages (rather than the oldest 50) to my new inbox? Thanks.

    Normally, the latest 50 is what you'd get.
    I suspect that, in moving the mails, their creation dates have been changed to the move date. The mails are physically moved one at a time, so their creation times will spread over the few minutes of the move. If they were moved starting with the most recent, then the most recent will have the earliest creation date, thus causing the confusion.
    A possible workaround would be to move them back to the old account, then recopy them in batches of 50-100, starting with the oldest. What might also work is to sort them in the mailbox by date, oldest at the top, and then move them all. That might fool the move into moving the old ones first

  • How to get a value from the previous element (XSLT/XPATH gurus ahoy!)

    Hi All,
    I am building an RTF template for a "letter of reference"-report. Sometimes there are several rows in the data, that need to be printed as one. This is due to consecutive temporary contracts, which will be printed out as one period of service.
    Here's a simplified data example to illustrate the problem.
    <ROW>
    <START_DATE>01-01-1980</START_DATE>
    <END_DATE>01-01-1988</END_DATE>
    </ROW>
    <ROW>
    <START_DATE>01-01-1988</START_DATE>
    <END_DATE>01-01-1990</END_DATE>
    </ROW>
    <ROW>
    <START_DATE>01-01-2000</START_DATE>
    <END_DATE>01-01-2005</END_DATE>
    </ROW>
    With the data above, I should print two lines:
    01-01-1980 - 01-01-1990
    01-01-2000 - 01-01-2005
    I need to compare START_DATE of an element (except for the first one) with the END_DATE of the previous element, to find out whether to print the END_DATE for that element or not. How can I get that value from the previous element?
    Thanks & Regards, Matilda

    use this to get the following End_date
    <?following-sibling::../END_DATE?>
    Try this
    <?for-each:/ROOT/ROW?>
    ==================
    Current StartDate <?START_DATE?>
    Current End Date <?END_DATE?>
    Next Start Date <?following-sibling::ROW/END_DATE?>
    Previous End Date <?preceding-sibling::ROW[1]/END_DATE?>
    ================
    <?end for-each?>
    o/p
    ==================
    Current StartDate 01-01-1980
    Current End Date 01-01-1988
    Next Start Date 01-01-1990
    Previous End Date
    ================
    ==================
    Current StartDate 01-01-1988
    Current End Date 01-01-1990
    Next Start Date 01-01-2005
    Previous End Date 01-01-1988
    ================
    ==================
    Current StartDate 01-01-2000
    Current End Date 01-01-2005
    Next Start Date
    Previous End Date 01

  • How to get the values multiple times for the specified element - ABAP IXML.

    Hi all,
             i have requirement to get the values for the specified elements multiple times. eg:., if the element is used in the xml 4 times then i need to get all the 4 values.  here is the sample xml,
    <View mmRelease="6.30" mmVersion="2.0" mmTimestamp="1180099591892" name="Comp_viewtestView" package="sap.com" masterLanguage="en">
    <AbstractView.InboundPlugs>
    <InboundPlug name="ip2">
    <ParameterizedFeature.Parameters>
    <Parameter name="ippara2">
    <Parameter.Type>
    <Core.ForeignReference modelName="DtDictionary" package="com.sap.dictionary" name="binary" type="DtSimpleType"/>
    </Parameter.Type>
    </Parameter>
    </ParameterizedFeature.Parameters>
    </InboundPlug>
    <InboundPlug name="hi">
    <ParameterizedFeature.Parameters>
    <Parameter name="hipara">
    <Parameter.Type>
    <Core.ForeignReference modelName="DtDictionary" package="com.sap.ide.webdynpro.uielementdefinitions" name="Visibility" type="DtSimpleType"/>
    </Parameter.Type>
    </Parameter>
    </ParameterizedFeature.Parameters>
    </InboundPlug>
    Here i need to store the following values in internal table, the element to get the values are,
    (1) InboungPlug name and the respective values of "Parameter name" and "Parameter type" name value.
    The output will be of 2 records & values should be
    Inbound plug name = ip2
    Parameter name    = ippara2
    Parameter type      = binary
    2nd record
    Inbound plug name = hi
    Parameter name    = hipara
    Parameter type      = Visibility
    These 2 records should be in internal table and then i will be passing to database table...
    Can anyone provide me the code for the above......
    Thanks in advance,
    Vishnu.

    I didn't get ur requirement..
    As per my understanding, upload the XML file into an internal table and then loop over the internal table and get the values for ptype and pname..
    XML ABAP  -  may b helpfull
    Edited by: Veeranji Reddy on May 7, 2009 2:10 PM

  • Have the oldest ipod touch. . daughter forgot password to enable it. It's now been disabled for 22K  minutes. I'm trying to reset as a new device, but iTunes is telling me it can't do anything w/it b/c it's "disabled". Does anyone know a way around this?

    Have the oldest ipod touch. . daughter forgot password to enable it. It's now been disabled for 22K  minutes. I'm trying to reset as a new device, but iTunes is telling me it can't do anything w/it b/c it's "disabled". Does anyone know a way around this?

    Read this
    iOS: Unable to update or restore

  • Type (..) of the context element (..) in view (..) does not exist

    Hi all
    I have got an question. I have an ABAP webdynpro with a context node which is connected to an ABAP structure.
    The context node is mapped to a context node of a view and the fields of the view are mapped to the context node of the view. So the data is send through them. That works fine.
    Within this ABAP structure i have added 3 extra new fields. After creating them i activated the structure.
    Within the webdynpro i have updated the context mapping between the node and the structure and also between the different mappings between the views and the componentcontroller where this node is used,
    Within the context tab of all the views i see all the three new fields.
    When i try to map those fields to a field in the screen it cannot be done. i get the following message:
    Type (..) of the context element (..) in view (..) does not exist
    Anf the new fields are gray.
    Does anybody know the solution for this?
    kind regards,
    Anton Pierhagen

    Hi all
    Thanks for all of the replies. But unfortunately the right answer wasn't there.
    I had tried to map the structure and re-create the node on several levels in my web-dynpro, but there was no change after this changes.
    When i created an own dataelement and an own domain in my ABAP structure, it worked.
    I could select them in the webdynpro. But why????
    i still not get it..
    But thanks for the replies!
    Kind regards,
    Anton Pierhagen

  • E4X: What happens to the root element?

    I'm working with XML using the E4X notation. I'm used to the "old school" XML APIs that use DOM and I'm a bit confused.
    For example, let's consider the following XML
    <mx:XML id="myXML" >
      <top>
        <sub1 id="uno">
          <sub2>hello</sub2>
          <sub3>world</sub3>
        </sub1>
        <sub1 id="duo">
          <sub2>ok</sub2>
          <sub3>ko</sub3>
        </sub1>
      </top>
    </mx:XML>
    In my code I use the following traces. This is done with the De MonsterDebugger, but all others should work the same way. I have casted the traces to String only to make it easier to ask this question (no need post images)
    MonsterDebugger.trace(this, "myXML:" + myXML);
    MonsterDebugger.trace(this, "myXML.sub1: " + myXML.sub1);
    MonsterDebugger.trace(this, "myXML.sub1.sub2: " + myXML.sub1.sub2);
    What I get as output is
    (String) = myXML:<top>
      <sub1 id="uno">
        <sub2>hello</sub2>
        <sub3>world</sub3>
      </sub1>
      <sub1 id="duo">
        <sub2>ok</sub2>
        <sub3>ko</sub3>
      </sub1>
    </top>
    (String) = myXML.sub1: <sub1 id="uno">
      <sub2>hello</sub2>
      <sub3>world</sub3>
    </sub1>
    <sub1 id="duo">
      <sub2>ok</sub2>
      <sub3>ko</sub3>
    </sub1>
    (String) = myXML.sub1.sub2: <sub2>hello</sub2>
    <sub2>ok</sub2>
    All is fine above and this was a bit long abstract for my question. The question is what happens to the root element, in this case <top>? I was trying to access the data with the following notation
    myXML.top.sub1; // etc
    I spent quite a lot of time trying to get it to work, until I discovered that the root element is not used. Can anyone explain this?
    Or, I guess the simple explanation is that the root node is ignored and the sub nodes are created as properties of the object. The question also could be stated as: Why isn't this documented in the Flex API Reference?
    Thanks.
    P.S. I found this article on common E4X pitfalls which also has other interesting topics when working with E4X.

    its gone
    downgrading would kill the phone..

Maybe you are looking for