I parse some stuff from a several urls and insert data in database. I want to do this now in parallel. I create a thread pool and run links in separate threads. But it isn’t safe. I decided synchronized method that get data from web and save it to database like this:
synchronized (this) {
Parser parser = new Parser(link);
feeds = parser.parse();
model.insertFeeds(feeds, link);
}
But this approach blocked access to web and database for other threads until current thread parse data and insert (this is 99% of his work), so it seems that they are perform more than one by one, not as a parallel.
Could you tell what is the better solution for things like this?
You should let the DB handle the concurrency for you and the way of controlling that is through JDBC transaction isolation levels