Java JDBC query in separate thread lock parent Java JDBC query in separate thread lock parent multithreading multithreading

Java JDBC query in separate thread lock parent


I think you have misunderstood a bit how threads work. The problem is that you are calling this on the parent thread:

myConnection.runQuery("Select * from hr.numbers order by dbms_random.value");

which is a sequential invocation of the runQuery method on the myConnection object that happens to be a thread. This does not mean that it will instruct the child to execute the method. Instead the parent will execute it itself and the child thread finishes as soon as its run method has returned.

If you want to have a separate thread that keeps receiving commands to execute queries you will have to implement a producer-consumer pattern, where the parent keeps queuing commands for the child to execute. To this end I recommend you look at ExecutorService.


When you call myConnection.runQuery(), you are inside the run() method of the parent thread, so you are not in the connection thread even if you call one of its methods.

You should call runQuery inside the run() method of myConnection to achieve what you want.


Your runQuery method is blocking since you call it on the main thread (So it will wait until the query is finished). If you want it to be called on another thread call it on the other's thread run method (just start the other thread where you would originally called for the query method)