Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:26:37+00:00 2026-06-18T04:26:37+00:00

Update : I’m on Java 1.6.34 with no chance of upgrading to Java 7.

  • 0

Update: I’m on Java 1.6.34 with no chance of upgrading to Java 7.

I have a scenario where I am only allowed to call a method 80 times per minute. It’s actually a service API written by a 3rd party, and it “shuts down” (ignores calls) its API if you call it too many times:

public class WidgetService {
    // Can only call this method 80x/min, otherwise it
    // it just doesn't do anything
    public void doSomething(Fizz fizz);
}

I’d like to write an ApiThrottler class that has a boolean canRun() method that will tell my Java client whether the doSomething(Fizz) method can be called or not. (Of course it can always be called, but there’s no sense of calling it if we’ve exceeded our rate.)

So something that would allow me to write code like so:

// 80x/min
ApiThrottler throttler = new ApiThrottler(80);

WidgetService widgetService = new DefaultWidgetService();

// Massive list of Fizzes
List<Fizz> fizzes = getFizzes();

for(Fizz fizz : fizzes)
    if(throttler.canRun())
        widgetService.doSomething(fizz);

This doesn’t necessarily have to be the API (ApiThrottler#canRun), but nevertheless I need a solid/reliable mechanism that will pause/sleep until WidgetService#doSomething(Fizz) can be called.

This makes me feel like we’re heading into the realm of using multiple threads, which makes me feel like we could use some sort of locking mechanism and the Java notification (wait()/notify()) model. But having no experience in this realm, I can’t seem to wrap my head around the most elegant solution. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:26:39+00:00Added an answer on June 18, 2026 at 4:26 am

    Probably one of the best of options would be to use Semaphore http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html class and give it 80 permits every minute. This can be accomplished for example by using timer class http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html.
    The caller thread will consume permits every time it performs the call to the service by calling acquire() on the semaphore, which will block if all permits have been drained already.

    It would of course be possible to code this up using wait/notify and integer counter with timer or separate thread, as you mention, but that would be more complex compared to the usage of more modern java.util.concurrent API that I have outlined above.

    It can look close to the following:

    class Throttler implements TimerTask {
      final Semaphore s = new Semaphore(80);  
      final Timer timer = new Timer(true);
    
      Throttler() {
        timer.schedule(this, 0, 60*1000);   //schedule this for 1 min execution  
      }
    
      run() {  //called by timer 
        s.release(80 - s.availablePermits());
      }
    
      makeCall() {
        s.acquire();
        doMakeCall();
      }
    
    }
    

    This should work starting from Java 5.

    Also even nicer solution would be to use com.google.common.util.concurrent.RateLimiter from Guava. It can look like this:

    class Throttler {
      final RateLimiter rateLimiter = RateLimiter.create(80.0/60.0);
    
      makeCall() {
        rateLimiter.acquire();
        doMakeCall();
      }
    }
    

    Semantics is slightly different compared to Semaphore solution, with RateLimiter being most probably better fit in your situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE I have mange to get it working by changing the Model call from
UPDATE 6/21/12 I have a form in rails that is working similar to an
UPDATE: Added doGet function. I have a web app that is used for a
UPDATE: I now have a solution I'm much happier with that, whilst not solving
Update There is no ready XML parser in Java community which can do NIO
Update: This only seems to be a problem at some computers. The normal, intuitive
UPDATE: I've spent way too much time on this and have decided to ditch
update I have moved the code to jsFiddle http://jsfiddle.net/TnSV6/13/ when edit is called the
Update: part of the problem might have been solved by accessing the data like
UPDATE #2: I have solved almost all my issues bar the one major one.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.