Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 523103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:25:22+00:00 2026-05-13T08:25:22+00:00

Let me go over what my code is doing (haven’t coded it yet), I

  • 0

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.

  1. looks for a record in the database that hasn’t been processed yet:

    SELECT TOP 1 * FROM Products WHERE isActive = 1
    
  2. looks up a URL (Rest call), returns back the HTML and stores it in the database

  3. 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?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:25:23+00:00Added an answer on May 13, 2026 at 8:25 am

    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:

    class virtualProduct {
    
      const time_t maxSync = 10;  // maximum age for unsynched d/b to row[]
    
      static struct {  // singleton
         int isActive;
         int urlRowId;
         etc ...
      } row [];
    
      static time_t lastSync;  // timestamp of last sync with d/b
    
      static mutex theLock;  // mutex to protect read/write of above
    
    
      function syncData()
      {
         lock (&theLock);
    
         // flush local updates to d/b
         foreach (row as item)
             if (item.updated)
             {
                   sql_exec ("update products set whatever = " + value + " where rowId = " + whatever);
                   if (okay)
                        item .updated = false;
             }
    
         // update from d/b (needed if other entities are updating it)
         sql_query ("select * from products");
    
         row [] = sql results;
         lastSync = now();
         unlock (&theLock);
      }
    
      function virtualProduct ()  // constructor
      {
          ...
          syncData();  // initialize memory copy of d/b
      }
    
      function ~virtualProduct ()  // destructor
      {
          syncData();  // write last updates
          ...
      }
    
      function UpdateItem(int id)
      {
         lock (&theLock);
         if (now () - lastSync > maxSync)
             syncData();
         int index = row.find (id);
         if (index >= 0)
         {
             row [index] .fields = whatever;
             row [index] .isActive = 0;
         }
         unlock (&theLock);
      }
    
      function ObtainNextItem()
      {
         lock (&theLock);
         if (now () - lastSync > maxSync)
             syncData();
         result = null;
         foreach (row as item)
             if (item.isActive == 1)
             {
                  item.isActive = 2;  // using Peter Schuetze's suggestion
                  result = item.id;
                  break;
             }
         unlock (&theLock);
         return result;
      }
    }
    

    There are still some minor wrinkles to fix like the double locking of the mutex in UpdateItem and ObtainNextItem (from calling into syncData), but that’s readily fixed when translating to a real implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 449k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here is a pretty easy alternative for splitting a text… May 15, 2026 at 8:12 pm
  • Editorial Team
    Editorial Team added an answer As of Python 2.6+ you should be using the subprocess… May 15, 2026 at 8:12 pm
  • Editorial Team
    Editorial Team added an answer Here's a tutorial on Creating Irregular Non-Rectangle Windows using WPF.… May 15, 2026 at 8:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.