-
RSS threads
Hello,
I'm trying to write RSS reader. What I want to do is: connect with few rss feeds, print news, get ttl field value (sleep time) and after that time do the same thing again. I think that thread is the best solution for that. My problem is, that when I print data from rss feeds there are mixed (eg. tittle from the first feed, description from the second). How can I prevent mixing the output?
below is the code of run() and main:
Code:
public void run() {
while (true) {
System.out.println("News from: " + u.toString());
try {
InputStreamReader isr = connect(port, u);
getNews(isr);
} catch (Exception e) {
System.err.println(e);
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ex) {
Logger.getLogger(RSS.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Code:
public static void main(String[] args) throws MalformedURLException, InterruptedException {
RSS rss = new RSS("http://rss.cnn.com/rss/edition.rss", 80);
rss.start();
RSS rss2 = new RSS("http://rss.cnn.com/rss/edition_world.rss", 80);
rss2.start();
}
-
I assume getNews() prints the feed?
Have a look at this:
Concurrent Programming with J2SE 5.0
Alternatively you could just collect all output in a e.g. StringBuffer and the print the whole StringBuffer at the end of getNews()
-
If you wanted them as a single lump (title/main body together) then you'll need a single thread to do the printing. At the moment you have two threads doing the printing.
You want the printing thread to have some form of queue, which will contain the list of news items to print. The rss threads will then add news items to this (synchronised) queue and the printing thread will remove them from the queue.
-
Hey guys, thank you for your replies.
[QUOTE]Alternatively you could just collect all output in a e.g. StringBuffer and the print the whole StringBuffer at the end of getNews()[QUOTE]
I think thats the simpliest solution:)
Is it somehow worse/slower then other ones?
-
Go on...write a queue....go on...you know you want to.
;)
But if a StringBuffer does what you need then...