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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With