如何简化笨拙的并发代码?

时间:2013-01-06 20:54:37

标签: java android multithreading concurrency locking

基本上这个代码执行http请求,如果http请求超时,它会重置wifi连接(有时必须这样做,就像它一样,我也可能是非Android相关的东西,而不是“重置wifi连接”)。

必须考虑以下特殊情况:

  • 如果1个或多个线程当前正在执行http请求,则不允许其他线程重置wifi连接
  • 如果1个线程当前已经重置wifi连接,而另一个线程即将重置wifi连接,则发送后一个线程直接重试http请求(当前线程完成重置wifi时)
  • 当wifi连接当前正在重置时不执行http请求
  • =>只有一个线程可以一次修复wifi连接,但多个线程可以发起http请求simutaneosly

让我头疼。

到目前为止,这是我的代码。我能改进什么?

static int requestsActive = 0;
protected int requestTry = 0;
static final int maxTrys = 2;
static final ReentrantLock wifiLock = new ReentrantLock();

public void evaluate() throws Exception {
try {
    requestTry++;
    while (wifiLock.isLocked()) // no not start new http request while wifi is being fixed
        Thread.sleep(400);
    requestsActive++; //increment so that another thread that wants to fix wifi knows it has to wait
    response = httpClient.execute(requestBase);
    requestsActive--; // when == 0 wifi can be fixed if it needs to
} catch (ConnectTimeoutException e) {
    requestsActive--; //same as above (for exception case)
    if (requestTry == maxTrys)
        throw new ConnectTimeoutException("maxTrys reached");
    if (!wifiLock.tryLock()) //another thread is currently fixing wifi, no need to do it myself
        evaluate(); // ...so start a new http request
    while (requestsActive > 0) // wait until no more threads are in the http request section above
        Thread.sleep(400);
    WifiManager wifiMan = (WifiManager) App.getContext().getSystemService(Context.WIFI_SERVICE);
    resetWifi(wifiMan); //reset android wifi, nothing special
    wifiLock.unlock();
    evaluate();
}

1 个答案:

答案 0 :(得分:1)

不幸的是,我不能保证这会有效,因为我没有安装Android模拟器并且很快将它放在一起。希望它至少作为一个概念来证明对你有所帮助。基本上它使用信号量允许一次尝试有限数量的请求,并且当连接超时时它将获得信号量的所有许可,这将阻止在重置wifi时发出任何新请求。这利用了Java代码库中已存在的并发代码,因此您不必自己重新实现任何代码。

您可以看到JavaDoc for Semaphores here

static final int MAX_CONCURRENT_REQUESTS = 10;
static final Semaphore httpRequestsLock = new Semaphore(MAX_CONCURRENT_REQUESTS, true);

public void evaluate() throws Exception {
    Foo requestBase = null;
    HttpClient httpClient = new HttpClient();

    httpRequestsLock.acquire();
    try{
        response = httpClient.execute(requestBase);
    }
    catch (ConnectTimeoutException e) {
        httpRequestsLock.release();
        httpRequestsLock.acquire(MAX_CONCURRENT_REQUESTS); // Blocks until all current requests are done
        WifiManager wifiMan = (WifiManager) App.getContext().getSystemService(Context.WIFI_SERVICE);
        resetWifi(wifiMan); //reset android wifi, nothing special
        httpRequestsLock.release(MAX_CONCURRENT_REQUESTS);
        evaluate();
    }
}
相关问题