I'm fairly new to multi threaded programming and I was hoping to get some insight into the best way to implement the following idea.

Right now my code works like this Current functionality

It is singularly threaded, so in the time it takes to process each piece of data and write it to the database, new data comes in and gets queued up which is slowing things down too much. I am running on a 4CPU server, but the current setup only uses 1.

I would like separate out the work done in the middle piece over the remaining 3 CPUs. How would I best do this? I thought that I could create a new thread for each new piece of data, but we are talking about over the course of a day hundreds of thousands of new threads. From what I read, the overhead associated with that would be very large. Memory is a concern for me so if the creation of all these threads eats too much memory I will be in trouble. Is it true that a new thread will utilize a less busy CPU, or will it use the same one since it is the same JVM?

The processing and DB write for each new piece of data should not take more than a couple of seconds, if that.

I was also reading about thread pools, but the idea is a bit confusing to me and I can't find a great example.

I was thinking something like this Idea

Please help a multi threaded newb out in coming up with a reasonable design! Thanks in advance :-)

Edit
Report