如何在多线程环境下实现FIFO队列
How to implement a FIFO queue in a multi-threaded environment
我正在尝试为 class 实现一个队列,为 url 获取 OpenGraph
数据。这个想法是,如果请求需要他们的 "proxy" 服务,OpenGraphIO
服务一次只允许一个请求。为了消除服务中的 "simultaneous proxy request" 错误,我想在名为 OpenGraphFetcherImpl
的服务 class 中实现一个请求队列。但是,我不知道如何在 fetch()
方法中实现实际的队列本身。显然fetch()
方法可以在多线程环境下调用。
我的classshell如下:
public class OpenGraphFetcherImpl implements OpenGraphFetcher {
private static final String[] domainsThatRequireProxy = {"instagram.com","facebook.com"};
private static final LinkedList<URL> proxyQueue = new LinkedList<>();
private final String api_key;
public OpenGraphFetcherImpl(String api_key) {
this.api_key = api_key;
}
/**
* Fetch OpenGraph information for a url. If the url is invalid or no data is returned, the OpenGraph
* object will be "empty" (non-null)
*
* Only one "proxy" request can be made at a time. Should a proxy be needed, the request will be queued
* and returned once previous requests have been completed.
*
* @param url end point to fetch OpenGraph data
* @return OpenGraph object
*/
@Override
@Nonnull
public OpenGraph fetch(URL url) {
if (useProxy(url)) {
// Clearly this code doesn't work, but logic should be to add the request to the queue and then make requests in FIFO order
proxyQueue.add(url);
return OpenGraphIO.fetchOpenGraphInfo(api_key, proxyQueue.poll(), true);
} else {
return OpenGraphIO.fetchOpenGraphInfo(api_key, url, false);
}
}
/**
* @param url url to test
* @return true if the host of the url matches any domains that require use of a proxy
*/
private boolean useProxy(URL url) {
return Arrays.stream(domainsThatRequireProxy).parallel().anyMatch(url.getHost()::contains);
}
}
根据您的描述,当 useProxy
为真时,您希望限制对 fetch() 的同步调用。然后,您可以使用一个对象来仅同步该案例:
public class OpenGraphFetcherImpl implements OpenGraphFetcher {
private static final Object fetchLock=new Object();
public OpenGraph fetch(URL url) {
if (useProxy(url)) {
synchronized(fetchLock) {
return OpenGraphIO.fetchOpenGraphInfo(api_key, url, true);
}
} else {
return OpenGraphIO.fetchOpenGraphInfo(api_key, url, false);
}
}
...
我正在尝试为 class 实现一个队列,为 url 获取 OpenGraph
数据。这个想法是,如果请求需要他们的 "proxy" 服务,OpenGraphIO
服务一次只允许一个请求。为了消除服务中的 "simultaneous proxy request" 错误,我想在名为 OpenGraphFetcherImpl
的服务 class 中实现一个请求队列。但是,我不知道如何在 fetch()
方法中实现实际的队列本身。显然fetch()
方法可以在多线程环境下调用。
我的classshell如下:
public class OpenGraphFetcherImpl implements OpenGraphFetcher {
private static final String[] domainsThatRequireProxy = {"instagram.com","facebook.com"};
private static final LinkedList<URL> proxyQueue = new LinkedList<>();
private final String api_key;
public OpenGraphFetcherImpl(String api_key) {
this.api_key = api_key;
}
/**
* Fetch OpenGraph information for a url. If the url is invalid or no data is returned, the OpenGraph
* object will be "empty" (non-null)
*
* Only one "proxy" request can be made at a time. Should a proxy be needed, the request will be queued
* and returned once previous requests have been completed.
*
* @param url end point to fetch OpenGraph data
* @return OpenGraph object
*/
@Override
@Nonnull
public OpenGraph fetch(URL url) {
if (useProxy(url)) {
// Clearly this code doesn't work, but logic should be to add the request to the queue and then make requests in FIFO order
proxyQueue.add(url);
return OpenGraphIO.fetchOpenGraphInfo(api_key, proxyQueue.poll(), true);
} else {
return OpenGraphIO.fetchOpenGraphInfo(api_key, url, false);
}
}
/**
* @param url url to test
* @return true if the host of the url matches any domains that require use of a proxy
*/
private boolean useProxy(URL url) {
return Arrays.stream(domainsThatRequireProxy).parallel().anyMatch(url.getHost()::contains);
}
}
根据您的描述,当 useProxy
为真时,您希望限制对 fetch() 的同步调用。然后,您可以使用一个对象来仅同步该案例:
public class OpenGraphFetcherImpl implements OpenGraphFetcher {
private static final Object fetchLock=new Object();
public OpenGraph fetch(URL url) {
if (useProxy(url)) {
synchronized(fetchLock) {
return OpenGraphIO.fetchOpenGraphInfo(api_key, url, true);
}
} else {
return OpenGraphIO.fetchOpenGraphInfo(api_key, url, false);
}
}
...