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 6931967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:38:45+00:00 2026-05-27T11:38:45+00:00

I have a webapp that needs to sometimes download some bytes from a url

  • 0

I have a webapp that needs to sometimes download some bytes from a url and package it up and send back to the requester. The downloaded bytes are stored for a little while so they can be reused if the same url is needed to be downloaded. I am trying to figure out how best to prevent the threads from downloading the same url at the same time if the requests come in at the same time. I was thinking of creating a class like below that would prevent the same url from being downloaded at the same time. If a url is unable to be locked then it either waits until its not locked anymore to try and download it as long as it does not exist after the unlock.

public class URLDownloader
{
    HashMap<String,String> activeThreads = new HashMap<String,String>();

    public synchronized void lockURL(String url, String threadID) throws UnableToLockURLException
    {
        if(!activeThreads.containsKey(url))
            activeThreads.put(url, threadID)
        else
            throw UnableToLockURLException()
    }

    public synchonized void unlockURL(String url, String threadID)
    {
        //need to check to make sure its locked and by the passed in thread
        returns activeThreads.remove(url); 
    }

    public synchonized void isURLStillLocked(String url)
    {
        returns activeThreads.contains(url); 
    }

}

Does anyone have a better solution for this? Does my solution seem valid? Are there any open source components out there that already do this very well that I can leverage?

Thanks

  • 1 1 Answer
  • 0 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-27T11:38:46+00:00Added an answer on May 27, 2026 at 11:38 am

    It sounds like you don’t need a lock, since if there are multiple requests to download the same URL, the point is to download it only once.

    Also, I think it would make more sense in terms of encapsulation to put the check for a stored URL / routine to store new URLs in the URLDownloader class, rather than in the calling classes. Your threads can simply call e.g. fetchURL(), and let URLDownloader handle the specifics.

    So, you can implement this in two ways. If you don’t have a constant stream of download requests, the simpler way is to have only one URLDownloader thread running, and to make its fetchURL method synchronized, so that you only download one URL at a time. Otherwise, keep the pending download requests in a central LinkedHashSet<String>, which preserves order and ignores repeats.

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

Sidebar

Related Questions

I have a webapp that I am in the middle of doing some load/performance
I have a Spring/Hibernate webapp that has some integration tests that run on an
I've got this webapp that needs some memory tuning. While I'm already profiling the
I have a Rails (2.3.8) webapp that needs to have the ability to list
I have an old .net 1.0 webapp that needs a bit of maintenance done
I'm facing the following problem: I have one module in my webapp that needs
We are developing a webapp that needs to send out emails written in Java/Groovy.
I have a web app for commercial property management that needs spreadsheet-like functionality for
I'm writing a simple web app in PHP that needs to have write access
I have a webapp that lists all of my artists, albums and songs when

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.