Encounter a comparison of the problem of egg, is the same time come over n concurrent calls, how to ensure that the simultaneous consumption of 30 minutes, and not more than 30 minutes of bad debts.
Reprint Please specify source: http://blog.csdn.net/supera_li/article/details/45506583
1. Problem Modeling:
N people call at the same time, 30 minutes remaining. Guaranteed 30 minutes of simultaneous consumption, after consumption, the phone will be forced to hang up. Without creating bad debts.
2. Solution:
Synchronization issues are not synchronized with the minimum start time. That is, I have to have a final threshold to control the last point time slices are not assigned to multiple users, only one user is supported online. Also, because you want to support a user, you must have a synchronization queue that maintains the order of requests to handle requests for rejection.
Assumed parameters:
Minimum start time parameter s=5.
N Synchronous Queue Q, which is the N-person queue that requests the server's remaining time per minute.
N Scale classification and time slice allocation algorithm:
1). n>30, the person after n-30 refused the request. The average distribution of 30 people is 1min.
2). n belongs to [30-s,30] interval, average n person assigns 1min
3). N<30-s, i.e. n belongs to (1,30-s) interval,
The first request n queues into q,q to detect if n overflows the remaining time, and the relationship between the remaining minutes after the next allocation is detected with the 30-s, and the DB is updated. Allocates a time allocation sum of n minutes.
Second allocation sum n*2 (assuming not hang up, continue into Q, perform detection judgment, update db)
The break termination condition is a detection failure. Complete the sync issue.
After the synchronization problem is complete, the remaining minutes are in the [30-s,30] interval and are not synchronized in accepting N requests. Only single-user calls are supported.
n+ the problem of synchronizing consumption, to discuss how to avoid bad business