Huaguoshan first apes placement test Record --Talk is cheap, Show me the code

This story is purely fictitious and any similarity is purely coincidental!

story background

Huaguoshan first placement test Record apes - apes, according to achieve the level determining counter

 

After Goku mentoring 4 people learn to come back, because intolerance receive discipline, returned to Flower and Fruit Mountain, led a gang of monkeys Monkey Sun unaffected live, it is regrettable because the monkey monkey Sun's book of life and death in the name of the palace of hell We are crossed out, and the monkey monkey Sun is growing.

Huaguoshan first placement test Record apes - apes, according to achieve the level determining counter

 

Wukong is the most impatient, unable to discipline, with the advice to Taibaijinxing.

Huaguoshan first placement test Record apes - apes, according to achieve the level determining counter

 

Apes placement test

Taibaijinxing to his mind: exam grade.

And given the title:

Create a universal counter, measurement can be a lot of things, such as the Monkey King Bar.

Huaguoshan first placement test Record apes - apes, according to achieve the level determining counter

 

Reference answer is as follows:

Apes in phases: a ~ nine levels rise sequentially

First order apes

public class Counter1 {
    private static int cnt=0;
    
    public int increase() {
        return ++cnt;
    }
    
    public int decrease() {
        return --cnt;
    }    
    
}

 

NARRATOR: to achieve the function.

Second-order apes

public class Counter2 {
    private static long cnt=0;
    
    public long increase() {
        return ++cnt;
    }
    
    public long decrease() {
        return --cnt;
    }        
}

 

旁白:考虑了int的范围限制,long的范围更广泛。

三阶猿类

public class Counter3 {
    private static long cnt=0;
    
    public synchronized long increase() {
        return ++cnt;
    }
    
    public synchronized long decrease() {
        return --cnt;
    }        
}

 

旁白:考虑了并发环境下的执行

四阶猿类

public class Counter4 {
    private static AtomicLong cnt=new AtomicLong(0);
    
    public long increase() {
        return cnt.getAndIncrement();
    }
    
    public long decrease() {
        return cnt.getAndDecrement();
    }        
}

 

旁白:考虑了并发环境下的cas性能更优

五阶猿类

public class Counter5 {
    private static LongAdder cnt=new LongAdder();
    
    public long increase() {
        cnt.increment();
        return cnt.longValue();
    }
    
    public long decrease() {
         cnt.decrement();
         return cnt.longValue();
    }        
}

 

旁白:在单线程下,并发问题没有暴露,两者没有体现出差距;随着并发量加大,LongAdder 的 increment 操作更加优秀,而 AtomicLong 的 get 操作则更加优秀。鉴于在计数器场景下的特点—写多读少,所以写性能更高的 LongAdder 更加适合。

六阶猿类

public class Counter6 {
    private static JdbcTemplateUtils jdbc=new JdbcTemplateUtils();
    private static long cnt=0;
    
    public long increase() {
        cnt=jdbc.getCnt();    
        return jdbc.setCnt(++cnt);
    }
    
    public long decrease() {
         cnt=jdbc.getCnt();
         return jdbc.setCnt(--cnt);;
    }        
}

 

旁白:考虑了在集群环境下保证数据的唯一性和一致性。

七阶猿类

public class Counter7 {
    private static RedisclusterUtils redis=new RedisclusterUtils();
    private static long cnt=0;
    
    public long increase() {    
        return redis.incr(cnt);
    }
    
    public long decrease() {
         return redis.decr(cnt);;
    }        
}

 

旁白:考虑了计数器集群下的并发性能问题,同样的实现可以使用zk或者mongo等内存数据库。

八阶猿类

public class Counter8 {
    private static JdbcTempalteUtils jdbc=new JdbcTempalteUtils();
    private static RedisclusterUtils redis=new RedisclusterUtils();
    private static long cnt=0;
    
    public long increase() {    
        if(redis.exsits(cnt)) {
            return redis.incr(cnt);
        }
        cnt=jdbc.getCnt(key);
        ++cnt;
        redis.set(key,cnt);
        
        return cnt;
    }
    
    public long decrease() {
        if(redis.exsits(cnt)) {
            return redis.decr(cnt);
        }
        cnt=jdbc.getCnt(key);
        --cnt;
        redis.set(key,cnt);
        
        return cnt;
    }        
}

 

VO: Consider the case where the processing redis down or otherwise unavailable, the backup program.

Nine bands apes

This should Exemption.

Huaguoshan first placement test Record apes - apes, according to achieve the level determining counter

 

References:

【1】https://mp.weixin.qq.com/s/yAvJFZWxfKb38IDMjQd5zg

Guess you like

Origin www.cnblogs.com/davidwang456/p/11550965.html