使用线程来使用Streaming API获取推文而不受速率限制

时间:2017-01-20 13:14:40

标签: java multithreading twitter twitter-streaming-api

我想使用线程来传输推文,但我得到了速度限制。我做错了什么?

public class AppThread {

public static void main(String[] args) {
     ExecutorService executor = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 5; i++) {  
            Runnable worker = new App("Thread" + i);  
            executor.execute(worker);//calling execute method of   ExecutorService  
          }  
        executor.shutdown();  
        while (!executor.isTerminated()) {   }  

        System.out.println("Finished all threads");  
}

在下面的类中,调用另一个类(未包含)来获取推文。

public class App implements Runnable {
private String threadName;
TwitterStream twitterStream = new     TwitterStreamFactory().getTwitterStreamInstance();
TwitterStreamWorker streamWorker = new TwitterStreamWorker();

public App(String name) {
    this.threadName = name;
}

public void appTwitter() {

    String[] queryParams = { "Delhi" };
    String[] queryLang = { "en" };
    FilterQuery filterQuery = new FilterQuery();
    FilterQuery query = filterQuery.getFilterQueryInstance(queryParams, queryLang);
    System.out.println("Query " + query);
    streamWorker.startStreamingWorker(twitterStream, query);
}

public void run() {

        System.out.println("Running thread " + threadName);
        appTwitter();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

public void start() {
    // TODO Auto-generated method stub

}
}

我也尝试过使用newSingleThreadExecutor()但问题仍然存在。

0 个答案:

没有答案