Let me go over what my code is doing (haven’t coded it yet), I want to make it run in multiple threads to speed things up.
-
looks for a record in the database that hasn’t been processed yet:
SELECT TOP 1 * FROM Products WHERE isActive = 1 -
looks up a URL (Rest call), returns back the HTML and stores it in the database
-
Sets the flag for that row:
UPDATE Products SET isActive = 0 WHERE rowId = 234
So say I wrap the above into a method:
public void UpdateProduct()
{
}
Is it possible to make this process run in multiple threads? (say 2 or 3)?
Update
Can someone show me a skeleton structure of how I would wrap this method into a multi-threaded process?
Since the slowest activity is html retrieval, this could be linearly sped up with 20, 50, or even 200 retrieval threads, depending on ISP bandwidth relative to the speed of the servers returning data.
It could be sensible to semi-virtualize the table to an in-memory array. So each thread looking for work would query a class member function which returns the next available row or handles updating it with being done. The class should also occasionally detects database updates if there are other updaters and flush in-memory updates back to the d/b every few seconds or minutes as makes sense.
I don’t know Java, so here is an impressionistic algorithm in PHPish lingo:
There are still some minor wrinkles to fix like the double locking of the mutex in
UpdateItemandObtainNextItem(from calling intosyncData), but that’s readily fixed when translating to a real implementation.