@Override public void run() { System.out.println("Connection to the GADM database..."); Connection conn; try { conn = GADM.connect_gadm(); } catch (SQLException e) { e.printStackTrace(System.err); return; } System.out.println("Connected, start processing"); String[] c; do { synchronized (to_process) { if (to_process.isEmpty()) { if (stop) break; try { to_process.wait(); } catch (InterruptedException e) { break; } continue; } c = to_process.removeFirst(); } try { process(conn, c[0], c[1]); } catch (SQLException e) { System.err.println("Error processing country " + c[0]); e.printStackTrace(System.err); } } while (true); System.out.println("Close GADM connection"); try { conn.close(); } catch (SQLException e) { e.printStackTrace(System.err); } synchronized (this) { stopped = true; this.notify(); } }