Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most effective way to multi-thread SQL (Java)

Firstly, I know this is slightly broad and opinion based but I just want a simple answer of the best practice of multithreading an application that uses SQL queries in Java.

I am making a program that needs to synchronize data from a MySQL database every iteration of the main thread. I would like to multithread this program so that a long query will not hold the main thread up and slow it's 'tick' rate.

I am not great at explaining the solutions I have came up with in words so I have made this image which I hope explains them a bit better. enter image description here

Are any of these ways the 'correct' way of doing things?

I recall something about possibly sending multiple queries at once then waiting for a result at the end, is this possible and how many queries should be sent at one time?

Should a separate thread be used for each query and if so how could I make this faster as I understand the overhead for creating a thread is quite large.

Thank you for reading my horribly worded and extremely long question, thanks in advance for any help.

like image 342
user2248702 Avatar asked Feb 14 '14 12:02

user2248702


People also ask

Is Java good for multi threading?

Java has great support for multithreaded applications. Java supports multithreading through Thread class. Java Thread allows us to create a lightweight process that executes some tasks. We can create multiple threads in our program and start them.

How can we achieve multithreading in Java?

We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface. Using runnable will give you an object that can be shared amongst multiple threads.

Can we run two threads simultaneously in Java?

Multithreading in Java is a process of executing two or more threads simultaneously to maximum utilization of CPU. Multithreaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java. Each thread runs parallel to each other.


1 Answers

MySQL can execute queries in parallel but not very much (I think 10-15). So I would create a pool of 10-15 threads, with common blocking queue for queries, each thread has its own database connection. Each working thread executes a loop: take next query, access DB, return result somehow. Play with number of threads to find optimum.

like image 147
Alexei Kaigorodov Avatar answered Sep 20 '22 02:09

Alexei Kaigorodov