Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!h25g2000prf.googlegroups.com!not-for-mail From: Robert Stark Newsgroups: comp.lang.java.programmer Subject: A quota based lock Followup-To: comp.lang.java.programmer Date: Mon, 8 Aug 2011 00:13:10 -0700 (PDT) Organization: http://groups.google.com Lines: 45 Message-ID: <83f81158-8aee-486d-a51b-c0f7dfdbb0da@h25g2000prf.googlegroups.com> NNTP-Posting-Host: 205.177.226.156 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1312787590 12225 127.0.0.1 (8 Aug 2011 07:13:10 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 8 Aug 2011 07:13:10 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: h25g2000prf.googlegroups.com; posting-host=205.177.226.156; posting-account=XnCmRgoAAABeRfo7PVUBJGDgHa3ZBNu6 User-Agent: G2/1.0 X-HTTP-Via: 1.1 SZXISA11-IN, 1.1 HKGOPR01-IN X-Google-Web-Client: true X-Google-Header-Order: VECRUHALSNK X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6862 I want to write a lock to control access to a resource, there are different kind of jobs using this resource, say job A,B,C, at the beginning, i use Lock api from jdk concurrent package, but i suffered from serious job starvation, so i want to do something like this: class T LockManager{ //the input is like this, A=>20, B=>70, C=>10, assign quota to different jobs public LockManager(Map quota){ } private void lock(T type){......} private void unlock(T type){........} public Lock getLock(T type){ return new QutaLock(type); } public class QutaLock implements Lock{ public QutaLock(T type){ ..... } public void lock(){.....} public void unlock(){.........} ...... } } My idea is input a map of percentages you want to assign for each job, and provide a simple lock api. Input A=>20, B=>70, C=>10 means A=>20%, B=>70%, C=>10% If there's no A jobs pending on the lock, then its quota would be divided evenly to other pending jobs that is B=>80%, C=>20%.(This rule apply to other type of jobs as well). Then i got stuck, the only way i can think about is to introduce an extra dispatch thread and several queues, can someone give me some hint?