Results 1 to 5 of 5
Thread: RSS threads
- 10-18-2010, 07:13 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 6
- Rep Power
- 0
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:
Java 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); } } }Java 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(); }
- 10-19-2010, 01:14 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 14
- Rep Power
- 0
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()
- 10-19-2010, 01:50 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 10-19-2010, 02:14 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 6
- Rep Power
- 0
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?
- 10-19-2010, 03:00 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Threads
By nsr2008.mca in forum New To JavaReplies: 7Last Post: 10-18-2010, 11:47 AM -
When to use threads
By simorgh in forum Threads and SynchronizationReplies: 2Last Post: 02-12-2010, 07:43 AM -
Threads!
By rameshraj in forum Advanced JavaReplies: 1Last Post: 05-04-2008, 04:11 PM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:25 AM -
Threads
By one198 in forum Threads and SynchronizationReplies: 1Last Post: 11-20-2007, 06:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks