Results 1 to 5 of 5

Thread: RSS threads

  1. #1
    morghul is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default 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();
    
        }

  2. #2
    Alessio is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    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()

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    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.

  4. #4
    morghul is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    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?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Go on...write a queue....go on...you know you want to.
    ;)

    But if a StringBuffer does what you need then...

Similar Threads

  1. Threads
    By nsr2008.mca in forum New To Java
    Replies: 7
    Last Post: 10-18-2010, 11:47 AM
  2. When to use threads
    By simorgh in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-12-2010, 07:43 AM
  3. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 04:11 PM
  4. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:25 AM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •