Using the code example provided by Twitter4j, I\'d like t stop the stream after a list of 1,000 status have been collected, and return this list. How can I do that?
There are two ways:
If you just need to finish the thread that consumes the stream (this is the thread that calls your listener) you can use twitterStream.cleanUp();. This will gracefully stop the thread. You might want to use a boolean stopped variable in your status listener so to ignore any calls that you will receive after this event.
You can also close the stream consuming thread along with its dispatcher thread (that is a deamon thread) by calling twitterStream.shutdown(); However this is a more brutal approach of ending communication with the twitter API. Though it works if you call this from inside the listener, I would prefer the approach suggested by @krishnakumarp