← 返回 uber 的题目列表Concurrent File Downloader with Request Coalescing
类型:online_judge
Problem: Concurrent File Downloader with Request Coalescing
Implement a thread-safe DownloadManager that supports concurrent URL download requests from multiple threads.
Given a function download(url) that performs the actual download and returns the downloaded result, implement:
get(url) -> result
Requirements:
If multiple threads request different URLs at the same time, the downloads should proceed concurrently and should not block each other unnecessarily.
If multiple threads request the same URL at the same time:
only one thread may actually call download(url);
all other threads must wait for the in-progress download;
all waiting threads must return the same result produced by the first download.
The result does not need to be cached forever. After the in-progress download finishes, a later request for the same URL may trigger a new download.
Exceptions must be handled correctly: if the actual download fails, all waiting threads should observe the same exception.
The implementation must be thread-safe and must not allow the same URL to be downloaded concurrently due to a race condition.
Input/Output Format for Testing
Input represents a batch of requests that start at the same time:
n
url_1
url_2
...
url_n
The program should execute these n requests concurrently. The simulated download function returns data(<url>) and records how many actual downloads happened per URL.
Output:
Print the result for each request in the original input order;
Print a line download_counts;
Print the actual download count for each URL in lexicographical order, using format <url> <count>.
Constraints
1 <= n <= 10^4
Each URL is a non-empty string of length at most 200
The implementation may choose the number of worker threads, but the DownloadManager itself must be thread-safe.
Example
Input
3
a
b
c
Output
data(a)
data(b)
data(c)
download_counts
a 1
b 1
c 1