# 妹纸小A的计数工作

### 九点半之前到公司人数

public class SimpleCounter1 {
public void check(long id) {
CheckRecordDO checkRecordDO = new CheckRecordDO();
checkRecordDO.setId(id);
}
public int count() {
return counter.size();
}
}


public class SimpleCounter2 {
final List<CheckRecordDO> counter = new LinkedList<CheckRecordDO>();
public void check(long id) {
synchronized (counter) {
CheckRecordDO checkRecordDO = new CheckRecordDO();
checkRecordDO.setId(id);
}
}
public int count() {
return counter.size();
}
}


public class SimpleCounter3 {
private CheckRecordDAO checkRecordDAO;
public void check(long id) {
CheckRecordDO checkRecordDO = new CheckRecordDO();
checkRecordDO.setId(id);
do {
if (checkRecordDAO.insert(checkRecordDO)) {
break;
}
} while(true);
}
public int count() {
return checkRecordDAO.count();
}
}


public class ParallelCounter1 {
final int ENTRY_COUNT = 5;
Counter[] counter;
{
counter = new Counter[ENTRY_COUNT];
for (int i = 0; i < ENTRY_COUNT; i++) {
Counter c = new Counter();
c.value = 0;
counter[i] = c;
}
}
public void check(int id, int entry) {
synchronized (counter[entry]) {
counter[entry].value++;
}
}
public int count() {
int total = 0;
for (int i = 0; i < ENTRY_COUNT; i++) {
total += counter[i].value;
}
}
public class Counter {
public int value;
}
}


public class ParallelCounter2 {
final int COUNTS = 16;
final int ENTRY_COUNT = 5;
Counter[] counter;
Integer[][] entryCounter = {
{0,0},
{1,1},
{2,3},
{4,7},
{8,15}
};
{
counter = new Counter[COUNTS];
for (int i = 0; i < COUNTS; i++) {
Counter c = new Counter();
c.value = 0;
counter[i] = c;
}
}
public void check(int id, int entry) {
int idx = choose(entry);
synchronized (counter[idx]) {
counter[idx].value++;
}
}
private int choose(int entry) { // 随机选择入口处的一个号码本
int low = entryCounter[entry][0];
int high = entryCounter[entry][1];
return low + (int)Math.floor(Math.random() * (high - low + 1));
}
public int count() {
int total = 0;
for (int i = 0; i < COUNTS; i++) {
total += counter[i].value;
}
}
public class Counter {
public int value;
}
}


### 前20人送咖啡券

public class SimpleCounter4 {
private CheckRecordDAO checkRecordDAO = new CheckRecordDAO();
public void check(long id) {
CheckRecordDO checkRecordDO = new CheckRecordDO();
checkRecordDO.setId(id);
checkRecordDO.setTime(new Date());
do {
if (checkRecordDAO.insert(checkRecordDO)) {
break;
}
} while(true);
}
public int count() {
return checkRecordDAO.count();
}
public void give() {
}
}


public class ParallelCounterWithCallback3 {
final int TOTAL_COFFEE_COUPON = 20;
final int COUNTS = 16;
final int ENTRY_COUNT = 5;
Counter[] counter;
boolean noMore = false;
final Integer[] coffeeCoupon = new Integer[COUNTS];
final Integer[][] entryCounter = {
{0,2},
{3,8},
{9,10},
{11,12},
{13,15}
};
{
counter = new Counter[COUNTS];
for (int i = 0; i < COUNTS; i++) {
Counter c = new Counter();
c.value = 0;
counter[i] = c;
coffeeCoupon[i] = (TOTAL_COFFEE_COUPON / COUNTS); // 平分
if (i < TOTAL_COFFEE_COUPON % COUNTS) {
coffeeCoupon[i] += 1;
}
}
}
public void check(int id, int entry, Callback cbk) {
int idx = choose(entry), get = 0;
synchronized (counter[idx]) {
if (coffeeCoupon[idx] > 0) {
get = 1;
coffeeCoupon[idx]--;
counter[idx].value++;
} else {
if (!noMore) { // 其他地方还有咖啡券
for (int i = 0; i < COUNTS && get == 0; i++) {
if (idx != i && coffeeCoupon[i] > 0) { // 找到有券的地方
synchronized (counter[i]) {
if (coffeeCoupon[i] > 0) {
get = 1;
coffeeCoupon[i]--;
counter[idx].value++;
}
}
}
}
if (get == 0) noMore = true;
}
if (noMore) counter[idx].value++;
}
}
cbk.event(id, get);
}
private int choose(int entry) { // 随机选择入口处的一个号码本
int low = entryCounter[entry][0];
int high = entryCounter[entry][1];
return low + (int)Math.floor(Math.random() * (high - low + 1));
}
public int count() {
int total = 0;
for (int i = 0; i < COUNTS; i++) {
total += counter[i].value;
}
}
public class Counter {
public int value;
}
public interface Callback {
int event(int id, int get);
}
}


∀m > n → Pim > Pin,

∃ m > n, Cim = 1 → Cin = 1

public class ParallelCounterWithCallback4 {
final int TOTAL_COFFEE_COUPON = 20;
final int COUNTS = 16;
final int ENTRY_COUNT = 5;
Counter[] counter;
final Integer[][] entryCounter = {
{0,2},
{3,8},
{9,10},
{11,12},
{13,15}
};
{
counter = new Counter[COUNTS];
for (int i = 0; i < COUNTS; i++) {
Counter c = new Counter();
c.value = 0;
counter[i] = c;
}
}
public void check(int id, int entry, Callback cbk) {
int idx = choose(entry), get = 0;
synchronized (counter[idx]) {
if (counter[idx].value < coupon(idx)) {
get = 1;
}
counter[idx].value++;
}
cbk.event(id, get);
}
private int coupon(int idx) {
int c = (TOTAL_COFFEE_COUPON / COUNTS); // 平分
return idx < TOTAL_COFFEE_COUPON % COUNTS ? c + 1 : c;
}
private int choose(int entry) { // 随机选择入口处的一个号码本
int low = entryCounter[entry][0];
int high = entryCounter[entry][1];
return low + (int)Math.floor(Math.random() * (high - low + 1));
}
public int count() {
int total = 0;
for (int i = 0; i < COUNTS; i++) {
total += counter[i].value;
}
}
public class Counter {
public int value;
}
public interface Callback {
int event(int id, int get);
}
}


### 小Z

• 本文已收录于以下专栏：

举报原因： 您举报文章：妹纸小A的计数工作 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)