public final int getAndIncrement(int id) {
for (;;) { int current = sql.query('select value from table where id = id'); int next = current + 1; if (compareAndSet(current, next)) return current; } } /*** expect 原来的值,update修改后的值,如果在修改前,有别的现成修改了这个值。那么expect发生改变,修改失败。 */ public final boolean compareAndSet(int expect, int update) { int up = sql.execute('update value = update table where value = expect') if(up > 0){ return true; } return false; }