I have a Jersey REST service method that accesses a cache. This method keeps track of the `requestsBeingProcessed` because a `MessageService` periodically updates the cache, but can only do so when there are no requests being processed. The calls to increment and decrement the number of requests being processed are synchronized to ensure thread-safe access.

```
class TeamInfoService {
    @GET
    @Path("/{teamId}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getTeamInfo(@PathParam("teamId") final int teamId) {
        MessageService.incrementRequestsBeingProcessed;
        String team = teamCache.getTeams().get(teamId);
        MessageService.decrementRequestsBeingProcessed;
        return team;
    }
}

class MessageService {
    private static int requestsBeingProcessed = 0;

    public synchronized static void incrementRequestsBeingProcessed() {
        requestsBeingProcessed++;
    }

    public synchronized static void decrementRequestsBeingProcessed() {
        requestsBeingProcessed--;
    }

    public synchronized static void getRequestsBeingProcessed() {
        return requestsBeingProcessed;
    }
}

```

The problem is that the `MessageService` has to acquire a lock to update the cache, but can only update it by checking `requestsBeingProcessed`, which can only be accessed by one thread at a time. 

```
public synchronized static void updateCache(String message) {
    while(getRequestsBeingProcessed() != 0) {
        //wait until there are no requests being processed
    }        
    processMessage(message);
}

```

I have a chicken/egg situation here: I can't get the lock to update `requestsBeingProcessed` because a lock is required to check `requestsBeingProcessed`. Is there a different way I should be approaching this problem?