Lock and semaphore, what's better method for my case

Hi all,
I'm implementing a classic case: a consumption queue with infinite capacity. That's say I have a queue of infinity capacity, a thread to put objects into the queue, another thread take it out. Pseudo code is smth like below:
void put(Object o) {
put message into the queue
if (consume thread is waiting) {
lock();
signal();
unlock();
void eat() {
if (queue not empty)
take object out;
else
lock;
wait for signal from producer;
wake up and take object out;
unlock;
I don't know if I should use semaphore or Lock interface to get the job done. Do you know which one is better in my case? I'm writing an apps which is very sensitive in latency so basically I want the eater to get the object as soon as possible. Any advices?
Message was edited by:
principles

Blocking queue doesn't work for me as it is too slow.
I don't need to lock the data because one thread
adds too the tail, one thread consumes the head, so
why bother?LinkedBlockingQueue allows concurrent access to the head and tail ie it uses two different locks.
A Lock is a mechanism for mutual exclusion. It generally has a notion of ownership and so the thread that locks must be the thread that unlocks.
A Semaphore is a resource counter. There are no constraints on which thread signals() after await(). It is only a protocol that you establish in your code that allows it to be used for exclusion.
A bounded-buffer generally needs two synchronization aids:
a) an exclusion control to ensure the data structure is not corrupted by concurrent access
b) a coordination control to allow consumers to block when the buffer is empty, or producers to block when the buffer is full.
These two can be combined by using "synchronized" methods and wait/notify. Or by using Lock and associated Condition objects. Or you can use "synchronized" blocks or Lock for exclusion, and handle the coordination using a seperate semaphore (which must use some form of internal synchronization too - but perhaps more efficient.)
If you have a lock-free data structure, such as ConcurrentLinkedQueue then you don't need anything for (a) and so you only need coordination. So try using a Semaphore: put() increments it and take() decrements it.
But the Semaphore still becomes a serialization point in your code.

Similar Messages

  • What is Licensing Method for SCCM and SCOM 2012

    What is Licensing Method for SCCM and SCOM 2012
    We have 75000 clients, so we going to implement SCCM and SCOM 2012 environment in azure. Actually we plan to go for One Central Administration site and three primary sites (to manage this clients), going to host in Azure. I need to know how the licensing
    process work for this environment, how many licenses we need to get for this? is charging for Client based, Site based or environment based?  Please update.
    Thank you
    Fazal
    Fazal(MCTS)

    Hi,
    Running the SCCM and SCOM servers themselves in Azure to manage clients outside azure is not really supported if I remember correctly..
    http://blogs.technet.com/b/configmgrteam/archive/2013/10/23/configmgr-and-endpoint-protection-support-for-windows-azure-vms.aspx
    Licensing is explained here.
    http://www.microsoft.com/licensing/about-licensing/SystemCenter2012-R2.aspx
    In short for Client OS you need a cal per client, included in Core CAL and Enterprise CAL.
    For servers it is licensed per CPU and can be licensed for all virtual servers on a host as well, included in the guide above.
    ps. side note a CAS is not really necessary in most case if you don't have more than 100'000 clients, it adds a lot of complexity and I would avoid it if I could.
    Regards,
    Jörgen
    -- My System Center blog ccmexec.com -- Twitter
    @ccmexec

  • I have removed the passcode from my ipad but still it is getting locked after sometime what can be reason for this?

    I have removed the passcode from my ipad but still it is getting locked after sometime what can be reason for this?

    Can you pls guide that when I am restoring my ipad from itunes do i need to restore as new or from backup?????
    Also is this problem  realted to using same apple id for two ipad..... Pls help and regarding ur suggesting of messing with my ipad , I will keep a check ..

  • Unable to update, when executed table gets lock and Execution does not stop even for an hour.

    Following is my Query, which unables to update. When Executed, table gets lock and Execution does not stop even for an hour.
    update Employees 
          set Status = 'Close'
          where statusid IN (select statusid 
                                             from MyView 
                                              where DownloadedDate ='2014-07-27 00:00:00.000'
    here Employee contains 3,00,000 of records and Subquery return 1,50,000 Empid 
    i tried in various ways but not able to solve, statusid  column have no index on,  i tried using cursor but it does not work.
    plz let me know how to solve this Issue, Its very Important to get solve as all my further work depends on Updation
    , THANKS IN ADVANCE.

    See also your other thread with the same question:
    http://social.msdn.microsoft.com/Forums/en-US/80243625-fc3b-4ee1-bce1-4adcdbc16c14/unable-to-update-when-executed-table-gets-lock-execution-does-not-stop-execution-even-for-an-hour?forum=sqlserversamples
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • What is better way for creating a thread? Why?

    A thread can be create in two way, extending Thread class or implimenting Runnable.
    What is better way for creating a thread? Why?

    Implementing Runnable
    See FAQ #1 here [http://forum.java.sun.com/ann.jspa?annID=9]

  • Help solve the problem, with the lifting of purchasing and payment card is locked, and now I can not pay for her purchases in the app store

    help solve the problem, with the lifting of purchasing and payment card is locked, and now I can not pay for her purchases in the app store

    Contact the App store for Apple ID help. Their support link is on the right of the App store window
    LN

  • I need to publish a magazine in arabic and english - what should I buy - for how much to use 15 mac books for my students to use indesign?

    I need to publish a magazine in arabic and english - what should I buy - for how much to use 15 mac books for my students to use indesign?

    You can only return the machine if you're within 14 days of purchase - otherwise you're just 'stuck' with what you purchased (unless you sell it, of course).
    I always recommend that Retina owners order 16GB of RAM as the RAM is not upgradable - it's soldered onto the logic board.
    Your call - if you're with 14 days of purchase, take it back and exchange for a 16GB model. Or sell it and buy a model with 16GB. The higher amount of memory will allow you to run far more memory-hungry apps concurrently.
    Good luck,
    Clinton

  • When bouncing- what's best method for smallest file size/highest quality?

    I am in the process of embedding 3 mp3's into a PDF to submit as a portfolio. The PDF also has text, and two scores included, and with the 3 embedded mp3's it can't be more than 10mb.
    So my question is: When bouncing a project out of Logic, what is the best method for getting the smallest file size, but retaining the best audio quality? And once it's out of Logic and it is an mp3 or other type of audio file, is there a best format for compressing it further, and still maintaining the relative quality?
    I bounced out the three projects into wav's. Now I am using Switch for Mac to compress them down to smaller Mp3's. I basically need them to be about 3 mb each. Two of the recordings sound OK at that size, but they are just MIDI(one project is piano and string quartet, the other is just piano- all software instruments. The recording that combines MIDI and Audio and has more tracks (three audio tracks and 10 Midi/software instrument tracks)and sounds completely horrible if I get it under 5 mb as an mp3. The problem is that I need all three to equal around 9mb, but still sound good enough to submit as a portfolio for consideration into a Master's program.
    If anyone can help I would really appreciate it. Please be detailed in your response, because I am new to logic and I really need the step by step.
    Thank you...

    MUYconfundido wrote:
    I am in the process of embedding 3 mp3's into a PDF to submit as a portfolio. The PDF also has text, and two scores included, and with the 3 embedded mp3's it can't be more than 10mb.
    So my question is: When bouncing a project out of Logic, what is the best method for getting the smallest file size, but retaining the best audio quality?
    The highest bitrate that falls within your limits. You'll have to calculate how big your MP3's can be, then choose the bitrate that keeps the size within your limit. The formula is simple: bitrate is the number of kilobits per second, so a 46 second stereo file at 96 kbps would be 96 x 46 = 4416 kbits / 8* = 552 kBytes or 0.552 MB. (*8 bits = 1 Byte)
    So if you know the length of your tracks you can calculate what bitrate you need to keep it within 10 MB total.
    I consider 128 kbps the lowest bearable bitrate for popsongs and other modern drumkit based music. Deterioration of sound quality is often directly related to the quality of the initial mix and the type of instruments used in it. Piano(-like) tones tend to sound watery pretty quickly at lower bitrates, as do crash and ride cymbals. But don't take my word for it, try it out.
    And once it's out of Logic and it is an mp3 or other type of audio file, is there a best format for compressing it further, and still maintaining the relative quality?
    You can only ZIP the whole thing after that, but that is just for transport. You'll have to unzip it again to use it. And no, you cannot compress an MP3 any further and still play it.
    I bounced out the three projects into wav's. Now I am using Switch for Mac to compress them down to smaller Mp3's.
    That is silly, you could have done that in Logic, which has one of the best MP3 encoders built in. And how good encoders are will especially come out at bitrates around or below 128, which you might be looking at.
    I basically need them to be about 3 mb each.
    So, one more scrap of info we need here: how long are those three pieces, exactly? I'll calculate the bitrate for you - but please bounce 'm directly out of Logic as MP3's. They will very probably sound better than your WAV-conversions made with Switch.
    !http://farm5.static.flickr.com/4084/4996323899_071398b89a.jpg!
    Two of the recordings sound OK at that size, but they are just MIDI(one project is piano and string quartet, the other is just piano- all software instruments. The recording that combines MIDI and Audio and has more tracks (three audio tracks and 10 Midi/software instrument tracks)and sounds completely horrible if I get it under 5 mb as an mp3. The problem is that I need all three to equal around 9mb, but still sound good enough to submit as a portfolio for consideration into a Master's program.
    Length of the piece? And does the .Wav bounce you have sound OK?

  • Oracle VM Manager and Agent 3.3 Beta Available for Testing

    We have the beta version of Oracle VM Manager available for testing. The beta software and documentation are available here.
    For SPARC customers, you'll need to download the beta versions of Oracle VM Agent for SPARC and DLM (Distributed Lock Manager) module to testing the new SPARC related management features.
    Please read the Welcome Letter to understand the requirements of the beta testing. We rely on our Beta Program participants to provide feedback on the usability, stability, and overall quality of our product release. This feedback will focus on your experience with the new features, product documentation, support services, and training materials. By providing in-depth feedback, you can help influence Oracle’s product direction.
    Provide us information about your set-up here.
    Periodically tell us what you have tried and how it is going here.

    Papi wrote:
    Ah, ok. But since this is to be a production system we will be installing a standar Oracle database as per documentation recommendation. In that case we should be safe... right?Yes, you would need to install SE or EE for production purposes. However, keep in mind that because XE is using the default ports, the new Database install will be using non-standard ports.

  • What are Payment Methods for the iPhone 5?

    I am looking at purchasing an iPhone 5 and was wondering what the payment methods were for the purchase?

    Apple does not offer such, but some carriers or authorized vendors may. You'll just have to check to see what's availble where you live.

  • What's the method for updating a book?

    I use a variation of FrameMaker 10's FM_Outputs_CondText.jsx to automate generation of PDFs.
    What do I need to add to force a book update?
    It looked to me as if it were doing that, but I just noticed that the build date variable in the front matter is updated only when I manually update the book.

    To be a little more elaborate: I checked the script you mentioned and it only updates cross-references when opening a book and its files. That is not the same as updating the book and all its variables. You do need to use the Book.UpdateBook method for that explicitly, after first opening all files in the book. That amounts to the same as manually updating the book.
    Ciao again

  • What is best method for transferring iTunes library from PC to iMac?

    I have a new iMac.  I have authorized it with my iTunes account.  My current iTunes library is on a Windows Vista PC, not on the same network.  What is the best method for me to use to transfer the iTunes library from the PC to the iMac?

    iTunes: How to move [or copy] your music to a new computer [or another drive] - http://support.apple.com/kb/HT4527
    Quick answer if you let iTunes manage your music:  Copy the entire iTunes folder (and in doing so all its subfolders and files) intact to the other drive.  Start iTunes with the option (shift on Windows) key held down and guide it to the new location of the library.
    Macworld - How to transfer iTunes libraries between PC and Mac - http://www.macworld.com/article/46248/2005/08/shiftitunes.html
    Move an iTunes library from a Windows PC to a Mac - http://www.macworld.com/article/1146958/move_itunes_windows_mac.html

  • What is class method for a float

    What is a class method for a float, and can I use it with a timer?

    X- Developer wrote:
    What is a class method for a float, ...
    A float is a simple data type that just stores one real number. Other simple data types are, for example, char, int, long, double, etc. None of these is a class. None have member variables, instance methods or class methods.
    The methods of some classes certainly use floats, and some methods have "float" in their name. For example, \[NSString floatValue\] will convert numeric text into a float value. Let us know if your question relates to a method like that. If so, remember to always specify which class you're asking about.
    can I use it with a timer?
    Any method can set up a timer which fires some other method once or repeatedly. However I don't understand what it means to use a float with a timer. In fact I don't know what it might mean if an int or a string or a view were used with a timer. Maybe it would help if you described what you're trying to do, ok?
    \- Ray

  • I'm looking for an app that will allow a presenter to give ipads to several other users in a room and control what's on display for the group

    please let me know if you know of any good apps to sync up several ipads in a room with one person in control. I'm looking for an app that will allow a presenter to control what's on display for several other ipad users in a room. I'd like to be able to do presentations over the ipad, but i want the presenter to control the screens so folks in the meetings don't get distracted by playing with the ipad. i'd like to avoid using a web meeting service and do this without having to connect to any kind of network since i'll often be doing this outside of my office.

    Try http://mysyncpad.com/. I'm not sure if it will do all you need, but it's the closest I've seen.

  • HT1677 When I am searching and enter what I am looking for all of my old emails are popping up. Is there a reason for this and way to remove it?

    While searching with safari when I am typing in what I am searching for all of my old emails pop in that are related to the subject. Is there a reason for this and or way to rid the search of this?

    Sometimes you want to just to search something that specific. Like if I am searching "ebay" I want the address and site for ebay to appear not everyone of my previous emails reguarding Ebay or searches in regards to ebay and this is all that pops up.

Maybe you are looking for

  • How to find out the Queries Generated for a particular ods?

    Hi all, There are atleast 10 queries generated for a Particular ODs zyyyyy. I would like to know what are the queries generated for a particular ods. I have used the where use list of the ods. But it did not displayed the queries generated for that o

  • All the attachments both in SRM and R/3 must be saved in IXOS database

    Dear all, I am having the following requirements: We are creating Shopping Cart in SRM with attachments and these attachments are saving in the IXOS database. All these (same attachments) are relpicating to R/3 whenever system generates PR or PO auto

  • Outbound IDOC - Sender and reciever

    Hello SDNites, I have created a report pgm which is equipped to send an outbound IDOC i.e used FM 'MASTER_IDOC_DISTRIBUTE'. Can someone please tell me how do I decide my sender and reciever when populating the control record in this FM. I know sender

  • Photoshop elements 12 trial

    I downloaded photoshop elements 12 trial version, after i sign in it wont let me do anything, it says im signed in but it just stays on same page, please help

  • Inserting a record into Access database

    Hi guys i am having problem with the insertion of a record in to a Access database!!!! It is not given any exceptions or errors but simply does not insert it!!! Here is my code!! import java.sql.*; class access public static void main(String a[]) //