title: Twitter streaming api - Restricting the number of tweets
body: <p>I am able to get the live twitter stream using the twitter streaming api like below</p>
  
  <pre><code>    ConfigurationBuilder cb = new ConfigurationBuilder();
      cb.setDebugEnabled(true);
      cb.setOAuthConsumerKey("xxxx");
      cb.setOAuthConsumerSecret("xxxx");
      cb.setOAuthAccessToken("xxxx");
      cb.setOAuthAccessTokenSecret("xx");
      int count = 0;
      TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
      StatusListener listener = new StatusListener() {
  
  
          public void onStatus(Status status) {
  
              System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
             // count++;
          }
  
  
          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
              System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
          }
  
          public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
              System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
          }
  
          public void onScrubGeo(long userId, long upToStatusId) {
              System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
          }
  
          public void onException(Exception ex) {
              ex.printStackTrace();
          }
  
          @Override
          public void onStallWarning(StallWarning arg0) {
              // TODO Auto-generated method stub
  
          }
      };
  
      FilterQuery fq = new FilterQuery();
      String keywords[] = {"samsung"};
      String lang[] = {"en"};
      fq.track(keywords);
      fq.language(lang);
      twitterStream.addListener(listener);
      twitterStream.filter(fq);   
  </code></pre>
  
  <p>How do I restrict the number of tweets? The above program is running indefinitely. How do I stop the program after getting the required number of tweets?</p>