Path: csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!nntp.cybernetik.net!usenet-01.nntp.cybernetik.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Fly Newsgroups: comp.lang.java.programmer Subject: streaming problem and thread freeze Date: Mon, 9 May 2011 07:56:15 -0700 (PDT) Organization: http://groups.google.com Lines: 105 Message-ID: <005a9e79-36fe-4bf1-aa1d-bda3a5aaebf2@glegroupsg2000goo.googlegroups.com> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 151.25.123.10 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1304952975 27339 127.0.0.1 (9 May 2011 14:56:15 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 9 May 2011 14:56:15 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=151.25.123.10; posting-account=E9yt5woAAAAjdPbKg8rPXAgEzMP_mDtP User-Agent: G2/1.0 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3858 Hello, I have a client program that read an XML from a STREAM every 60 seconds. From the main Thread I do an infinite loop where I create a new working thread executing all the operations: read, parse, etc.. This is what I do: DownloaderTask tempTask = null; while (true) { if (tempTask != null) { if (tempTask.isAlive()) { tempTask.cancel(); } } tempTask = new DownloaderTask(); tempTask.start(); try { sleep(frequency * 1000); } catch (InterruptedException ex) { log.severe("Main sleep failed"); } } Inside the Run method of the DownloaderTask, I first read from the Stream (a class field) with a BufferedReader, in this way: private String readDocumentFromStream(BufferedReader reader) throws IOException { char[] buffer = new char[4 * 1024]; int charsRead = -1; String retVal = ""; log.finer("---START reading XML ---"); while ((charsRead = reader.read(buffer, 0, 4 * 1024)) != -1) { log.log(Level.FINER, "CharsRead: {0}", charsRead); retVal += String.copyValueOf(buffer, 0, charsRead); } log.finer("---STOP reading XML---"); return retVal; } In the DownloaderTask class I wrote a cancel() method, called by the main thread in the case the working thread was still alive after the sleep(). It's definition: synchronized public void cancel() { this.interrupt(); try { if (rd != null) { rd.close(); } } catch (IOException ex) { log.severe(ex.getMessage()); } http_conn.disconnect(); log.log(Level.SEVERE, "Timeout. Thread {0} is being canceled and resource released.", this.getId()); } My problems. Sometimes I see my program active, doing nothing, blocked probably in the read(), because if I check all logs, the last line written is "CharsRead: xxx" and no more. Here I post one of my saved log (where the read buffer was 1024 bytes): ||-- 2011-05-03 06:44:59 FINE: Updating events data... --|| ||-- 2011-05-03 06:44:59 FINE: Events updated! --|| ||-- 2011-05-03 06:44:59 INFO: Release date: 2011-05-03 06:45:15 | Received: 187 Inserted: 0 Updated: 0 Deleted: 0 ||| Errors: 2 --|| ||-- 2011-05-03 06:44:59 FINER: Normal closing thread 1.723 --|| ||-- 2011-05-03 06:44:59 FINER: Thread 1.723 Closing --|| ||-- 2011-05-03 06:45:52 FINER: Thread 1.725 Opening --|| ||-- 2011-05-03 06:45:52 FINE: Opening server connection to: mysite.com:80 --|| ||-- 2011-05-03 06:46:19 FINE: Reading From Stream --|| ||-- 2011-05-03 06:46:19 FINER: ---START reading XML --- --|| ||-- 2011-05-03 06:46:19 FINER: CharsRead: 1.024 --|| ||-- 2011-05-03 06:46:19 FINER: CharsRead: 1.024 --|| ||-- 2011-05-03 06:46:19 FINER: CharsRead: 1.024 --|| ||-- 2011-05-03 06:46:19 FINER: CharsRead: 929 --|| ||-- 2011-05-03 06:46:19 FINER: CharsRead: 1.024 --|| ||-- 2011-05-03 06:46:19 FINER: CharsRead: 1.024 --|| ||-- 2011-05-03 06:46:19 FINER: CharsRead: 872 --|| I noticed another strange thing in this log, that is I can't see the Thread Id: 1724. Can you help me to find where I am wrong? In the logic or maybe in the syntax of the program? Thanks in advance, Flavio