0

I have a Java program that employs multi-threads using pool of threads. Each thread is going to make some select and insert statements. The DB is MySQL InnoDB.

I am planning to launch several run screens (6 processes), and each process is going to employ multi-threads. My questions are:

1) How many threads can I define for the pool? Am I limited to maximum of 4 threads since my processor is Core i7-2640M ?

2) Should I worry about anything regarding the DB integrity ? I did not use synchronization for anything in my program (thre is no need) but I am worried about the DB insert statement. Do I need to synchronize them or MySQL will take care about that knowing that I have one connection for each process in the main function.

Please, advise me.

2 Answers 2

1

Starting with 2) DB integrity is guaranteed, as long as you watch your isolation levels: If your inserts are not atomic, or your selects care about insert timimng from the other threads, use transactions.

Concerning 1) The optimum number of threads is heighly dependant on what these threads do: A thread waiting for a remote database transaction to complete definitly does not need a core available. I recommend you start with something like 4 times the core count and start testing from there. Depending on your OS you will also want to benchmark processes vs. threads.

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

Comments

0

Regarding thread amount

You can have 100 threads on a single core if you like. The OS will schedule them so each get some time to run. A running computer will already have a few hundred active threads / processes, most of them are just idle (Windows task manager shows that info somewhere).

Each core in your CPU can run 1 thread at a time. The i7-2640M has two physical cores but each does hyperthreading (= running 2 threads on 1 physical core in "parallel") so it provides 4 cores for you to work with.

To approximate the optimum thread count you need to know how active your threads are. If each thread runs all the time without sleep / wait it takes up a complete core. You should not use more than 4 threads in that case since they will just block each other. Switching between active threads takes some time for the CPU so more threads will result in less performance overall.

If your threads are waiting, you need to figure out the percentage of activeness. E.g. each screen-thread waits for user input most of the time. Each input takes in 1 second to process and you expect that each screen will get 30 events per minute. That would mean you need about 30 seconds per minute for each. You have 4 x 60 seconds per minute so your optimum thread / screen count would be around 8.

If you have an executor to run small tasks, each completing within seconds, then use about as much threads as you have cores. Small tasks can be distributed by the executor to threads so that you get back to about 100% load for each core.

Regarding Db integrity

see Eugen Rieck. Transactions are the way to ensure atomic modifications (= no other process can see a state in between transaction begin and end)

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.