I am working on a spring boot project. I have a scenario where I need to make a service call, fetch data from DB and update a response object. I want to do these in two different threads in order to achieve better performance.
I tried using the following code
Thread t1 = new Thread(new Runnable(){
public void run(){
try {
//make service call and update response object.
}
catch(Exception e){
}
}
});
t1.start();
//fetch data from DB and update the response object.
t1.join();
But this does not work when I try to hit the request and let it complete without putting any break points(basically executing it in the sequence that I want).
Is there a better alternative to achieve this?
Can I use fork and join here? If yes, how do I use it?
Please note that the data from service call and data from DB are not dependent on each other.
On further digging I found the below information in oracle documentation
http://www.oracle.com/technetwork/articles/java/fork-join-422606.html
First and foremost, fork/join tasks should operate as “pure” in-memory algorithms in which no I/O operations come into play. Also, communication between tasks through shared state should be avoided as much as possible, because that implies that locking might have to be performed. Ideally, tasks communicate only when one task forks another or when one task joins another.