Rate Limit Prevention [Twitter4J]
<p>I am currently implementing a program which requires me to use Twitter4J to collect tweets and store them.  However, I do realise that you can only make 180 requests every 15 minutes with Twitter's Developer API. </p>

<p>Due to this, I have created a method that stops the program after it gets 10 tweets, for 15 minutes while my consumer and access keys reset the rate limit.  However, sometimes the Rate Limit still manages to run out in between getting those 10 tweets? So what I want to do is change the method so that it stops due to the rate limit about to run out. </p>

<p>For example...</p>

<pre><code>if (rate limit = 0){
   stop program until rate limit resets
}
</code></pre>

<p>However, my method just now just implements a counter and when that counter reaches 10, it stops, which isn't very economical or effective.  I thought 10 tweets would be an adequate amount but obviously not.
Here is my method...</p>

<pre><code>public void getRateLimit() throws TwitterException{
        if (count == 10){
            try {
                System.out.println("Rate Limit is about to be exhausted for resource...");
                System.out.println("Please wait for 15 minutes, while it resets...");
                TimeUnit.MINUTES.sleep(15);
                count = 0;
            } catch (InterruptedException e) {
                System.out.println(e);
            }
}
</code></pre>

<p>How could I alter this so that it runs out when the rate limit is about to run out and stops, and only starts when it is replenished.
Thanks for any help.</p>