1

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?

4
  • 1
    I'm not sure what you're really trying to achieve. It's not safe for multiple threads to access the database at the same time, but when they access it sequentially you're upset that it's not parallel? What access pattern are you looking for? Commented Nov 2, 2012 at 12:44
  • I would change the methods so they can be run concurrently without synchronization. Commented Nov 2, 2012 at 12:44
  • 1
    Please explain what conflict you are trying to prevent. You are generally better off using transactions in DBMS to prevent data conflicts Commented Nov 2, 2012 at 12:45
  • Thanks for your attention. I had a one object in each threaf that do all job that code shows but now I create each own instance in each thread to prevent concurrency. Does it well? Commented Nov 2, 2012 at 13:25

1 Answer 1

5

You should let the DB handle the concurrency for you and the way of controlling that is through JDBC transaction isolation levels

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.