/* Daniel Shiffman */ /* Programming from A to Z */ /* Spring 2006 */ /* http://www.shiffman.net */ /* daniel.shiffman@nyu.edu */ // A Simple Thread example // Reads information from an XML feed every so often // occuring to the "wait" variable import java.util.ArrayList; import org.w3c.dom.Element; import org.w3c.dom.Node; import a2z.*; // Lookie, our code is a Processing library! public class NewsThread extends Thread { private boolean running; // Is the thread running? Yes or no? private int wait; // How many milliseconds should we wait in between executions? private ArrayList headlines; // ArrayList that will hold the news headlines private boolean available; // Is new news available? // Constructor, create the thread // It is not running by default public NewsThread (int w, int total){ wait = w; running = false; available = false; headlines = new ArrayList(); } // Overriding "start()" public void start () { // Set running equal to true running = true; // Print messages System.out.println("Starting thread (will execute every " + (wait/1000) + " seconds.)"); // Do whatever start does in Thread, don't forget this! super.start(); } // We must implement run, this gets triggered by start() public void run () { while (running){ System.out.println("reloading. . ."); check(); // Ok, let's wait for however long we should wait try { sleep((long)(wait)); } catch (Exception e) { } } } // Our method that quits the thread public void quit() { System.out.println("Quitting."); running = false; // Setting running to false ends the loop in run() // We used to need to call super.stop() // We don't any more since it is deprecated, see: http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html // super.stop(); // Instead, we use interrupt, in case the thread is waiting. . . super.interrupt(); } public boolean available() { return available; } public synchronized ArrayList getHeadlines() { // We should put a while (!available) loop here // but since we are explicitly only calling this function if available is true, we're ok available = false; notifyAll(); // let's notify everyone that available has changed return headlines; } private synchronized void check() { headlines = new ArrayList(); // Create a URL object and open an InputStream A2ZXmlReader xmlreader = null; try { xmlreader = new A2ZXmlReader("http://itp.nyu.edu/icm/proxy/proxy.php?url=http://news.google.com/?output=rss"); // Call our recursive search function to locate the element we want ArrayList elements = new ArrayList(); xmlreader.fillArrayList(xmlreader.getRoot(),"title",elements); for (int i = 0; i < elements.size(); i++) { Element e = (Element) elements.get(i); // As long as we find the element if (e != null) { Node n = e.getFirstChild(); String headline = n.getNodeValue(); headline = headline.replaceAll("'","'"); if ((!headline.matches("Google News")) && (headlines.size() < maxheadlines)) { headlines.add(headline); } } } available = true; notifyAll(); // let's notify everyone that the headlines have been updated } catch (Exception e) { System.out.println("Something went wrong. " + e); } } }