Krishna Chaitanya
Krishna Chaitanya

Reputation: 2663

How to return values from a list in round robin in a multi threaded environment?

I have to write a code which involves making a web service call to a backend asynchronously and returning a toll free number in round robin from a list of TFNs to the caller. Below is my code

@Stateless
public class TollFreeServiceBean {
    @EJB
    private AsyncBackendService asyncBean;
    public long getTollFreeNumber(Request request) {
        //Validate request 
        asyncBean.callBackend(request);
        return TFNUtil.getNext();
    }
}

@Stateless
public class AsyncBackendService {
    @Asynchronous
    public void callBackend(Request request) {
        // Call web service and inform a caller with `Request` is going to call
    }
}

public class TFNUtil {
    private static int index = 0;
    private TFNUtil(){}
    public static synchronized long getNext() {
        // Get TFN list from cache
        if(tfnList.size() >= index) {
             index = 0;
        }
        long tfn = tfnList.get(index);
        index++;
        return tfn;
    }
}

The idea is that the customers will get a different TFN each until the TFNs in the cache are completed. Once completed, the next customer should get the first TFN again. For example if there are 10 TFNs and 11 customers, 11th customer should receive 1st TFN. 12th customer should receive 2nd TFN and so on.

The backend system is complaining that two different simultaneous customers are landing on the same TFN. So I think my TFNUtil.getNext() logic is not right.

Can anyone please point out the bug here?

Upvotes: 2

Views: 348

Answers (1)

GhostCat
GhostCat

Reputation: 140613

Your statements are contradicting themselves.

On the one hand, you are worried that no two customers should receive the same number.

On the other hand, you purposely put a system into place that will exactly do that.

You see, depending on how many elements your list tfnList has ... your method getNext() might "spill over" pretty quickly. And what do you think will happen then?

To me, it sounds like the real solution is more complex: you might have to re-design your system. Instead of just rotating that list of numbers, you might need a system that is able to block/reserve a number for some time. And while a number is reserved, it is not handed out again.

A simple version of that would be "time" based only; meaning that a reservation "vanishes" automatically after some X minutes/hours. If that doesn't work; you would have to go one step further: and find something/somebody in your workflow that to unreserve numbers to render them "available" again.

And beyond that, it seems that you didn't fully consider what should happen when your have N numbers available, but N+x customers coming in at the same time! No round-robin'ing/rotating ... whatever can help you there! If that situation is possible, then you have to deal with it and define what should happen then!

Upvotes: 1

Related Questions