• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads Blocked  RSS feed

 
Alex Jordan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I developed a small crawler application. This is a multi thread application. This application was working fine from last 2.5 days but suddenly all threads blocked.

I checked its status through JConsole and found

Total # of Threads 10

_______________________Thread # 1,2,3,4,5,6,7,9,10 Status_________________________________

Name: Crawler 7
State: BLOCKED on java.io.PrintStream@127afe7 owned by: Crawler 8
Total blocked: 61 Total waited: 9

Stack trace:
java.lang.Throwable.printStackTrace(Throwable.java:461)
java.lang.Throwable.printStackTrace(Throwable.java:451)
PageFetcher.fetch(PageFetcher.java:229)
CrawlerThread.downloadPage(CrawlerThread.java:78)
CrawlerThread.run(CrawlerThread.java:63)
java.lang.Thread.run(Thread.java:619)


_______________________Thread # 8 Status_________________________________
Name: Crawler 8
State: RUNNABLE
Total blocked: 43 Total waited: 5

Stack trace:
java.io.FileOutputStream.writeBytes(Native Method)
java.io.FileOutputStream.write(FileOutputStream.java:260)
java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
- locked java.io.BufferedOutputStream@8d9051
java.io.PrintStream.write(PrintStream.java:432)
- locked java.io.PrintStream@127afe7
sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
- locked java.io.OutputStreamWriter@1fdb82c
java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
java.io.PrintStream.write(PrintStream.java:477)
- locked java.io.PrintStream@127afe7
java.io.PrintStream.print(PrintStream.java:619)
java.io.PrintStream.println(PrintStream.java:756)
- locked java.io.PrintStream@127afe7
java.lang.Throwable.printStackTrace(Throwable.java:464)
- locked java.io.PrintStream@127afe7
java.lang.Throwable.printStackTrace(Throwable.java:451)
PageFetcher.fetch(PageFetcher.java:229)
CrawlerThread.downloadPage(CrawlerThread.java:78)
CrawlerThread.run(CrawlerThread.java:63)
java.lang.Thread.run(Thread.java:619)


Here is the code of PageFetcher which is causing this issue


At Pagefetch:229 I have e.printStackTrace();

I am already using log4j for logging. I am not able to understand reason behind this issue as this statement "e.printStatckTrace()" exceuted many time since crawler started but suddenly everything blocked because of this.

Regards, Alex


 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Exception.printStackTrace writes to the standard err stream.
Java uses FileOutputStream and PrintWriter objects to write to this stream - these classes are not synchronized.
It depends on the operating system what happens if you write to the same err stream from several threads at once,
could be that the writing operation hangs at the operating system level.
Log4j is able to print stack trace and is fully synchronized, use for example logger.trace( "Message", exception) call,
if you want to print these traces on the console, you can configure and use ConsoleAppender from j4log.

Regards
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i may be way off base, but can't you synchronize the code causing the problem in a method or something? even if it is just one line. there is no rule saying methods must have more than one line of code.

for example:
void synchronized printStack(Exception e)
{
e.printStackTrace();
}

then in the main code, instead of e.printStackTrace();
you say printStack(e);

that way only one thread can do it at a time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!