0

In my program there are 3 threads. Thread 1 is responsible for populating records from MYSQL database which has currently 279381 records into queue. Thread 2 and Thread 3 read and process records from queue and insert them into the HSQL database. As per my expectoration, I got OutOfMemoryError on Thread 1 but to my surprise instead of terminating my process, my Thread 3 is still running. Isn’t it a weird behavior? I always thought that Errors are fatal. They are at jvm level and program gets terminated on their occurrence. But I think that Errors are Thread specific and If program can continue even on their occurrence, why we are not allowed to catch them ? Or there is still something about which I am unaware of? I expect experts will show some light on it.

I am attaching the console output.

LL populated :: 558759
Rows Read from Hospital Database ::279381
Total Rows in HSQL :: 119699    8931464
In Read HSQL
LL populated :: 558759Exception in thread "Thread-1" java.lang.OutOfMemoryError: Java heap space
    at java.nio.CharBuffer.wrap(CharBuffer.java:369)
    at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:265)
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
    at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
    at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
    at java.io.PrintStream.newLine(PrintStream.java:545)
    at java.io.PrintStream.println(PrintStream.java:807)
    at XXXX.readResultSet(XXXX.java:196)    
    at java.lang.Thread.run(Thread.java:722)
java.sql.SQLException: java.lang.OutOfMemoryError: Java heap space
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
    at YYYY.readHSQL(YYYY.java:203)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.hsqldb.HsqlException: java.lang.OutOfMemoryError: Java heap space
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.result.Result.newErrorResult(Unknown Source)
    at org.hsqldb.StatementDMQL.execute(Unknown Source)
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 6 more
Caused by: java.lang.OutOfMemoryError: Java heap space
    at org.hsqldb.QuerySpecification.buildResult(Unknown Source)
    at org.hsqldb.QuerySpecification.getSingleResult(Unknown Source)
    at org.hsqldb.QuerySpecification.getResult(Unknown Source)
    at org.hsqldb.StatementQuery.getResult(Unknown Source)
    ... 10 more

In Read HSQL
Total Rows in HSQL :: 119699    8931464
In Read HSQL
Total Rows in HSQL :: 119699    8931464
In Read HSQL
sailor
  • 753
  • 1
  • 6
  • 17

1 Answers1

1

There is nothing magical about thrown Errors, except they means something serious has happened, like out of memory. You you don't usually want to catch them, that's it.

Uncaught exceptions thrown inside a thread stops the thread itself. Other threads are not affected. There is a way to catch them though, see this question.

Community
  • 1
  • 1
Martin Wickman
  • 19,662
  • 12
  • 82
  • 106