c******n 发帖数: 4965 | 1 the basic framework of a connection pool code,
we modified it several times, now it seems to work fine, but I want to make
sure it's really correct
the greatly simplified version looks like the following.
n_loaned is actually an AtomicInteger, I use the "++" notation for
easier reading.
let's say we have 10 threads sharing the same pool obj, they all run the
sequence
"borrowConnection();
// do something for a while ;
releaseConnection()
"
the question is
is it possible that all of them (or any of them) would be blocking on the
line "block_forever()" ??, it seems impossible, but it's difficult to give
a formal proof given multi-threads.
class pool {
n_loaned = 0;
public Connection borrowConnection() {
if ( n_loaned == 10 )
block_forever();
n_loaned++;
}
public void releaseConnection(conn) {
n_loaned--;
}
} |
|