This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ComputeUtil { | |
/** | |
* 根据已有平均值、总量、新增值递推新平均值的公共函数, 计算公式: newAvg = (oldAvg * oldCount)/(oldCount+1) + newVal/(oldCount+1); | |
* 另外由于目前的需求是保留2位小数,所以当oldCount大到一定程度时,oldCount/(oldCount+1) 趋近于 1 且 1/(oldCount+1) 趋近于 0,就不必执行某些实际计算以减少不必要操作 | |
* | |
* @param oldAvg 老平均值 | |
* @param oldCount 老的总量 | |
* @param newVal 新值 | |
* @return 新平均值 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1.查找相同主键对象 | |
DBObject old = col.findOne(qo); | |
if (old == null) { | |
// 2.若不存在,尝试插入, 若出错"duplicate 主键",则转到3.更新操作 | |
try { | |
col.insert(newObj, WriteConcern.SAFE); | |
} catch (DuplicateKey e) { | |
// 2.1 瞬间又被插了一条记录,尝试更新操作 | |
BasicDBObject upObj = new BasicDBObject(); | |
WriteResult rst = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns | |
Compress 1K bytes with Zippy 3,000 ns // 3微秒 | |
Send 2K bytes over 1 Gbps network 20,000 ns // 20微秒 | |
Read 1 MB sequentially from memory 250,000 ns // 0.25毫秒 | |
Round trip within same datacenter 500,000 ns // 0.5毫秒 | |
Disk seek 10,000,000 ns // 10毫秒 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A data structure which have both stack & set features, especially useful to | |
* detect 'circles' in trees or graphs. | |
* | |
* @author pf-miles | |
* | |
*/ | |
public class SetStack<E> extends LinkedHashSet<E> { | |
private static final long serialVersionUID = -3159906489148656808L; | |
private Deque<E> innerStack = new ArrayDeque<E>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import com.github.pfmiles.dropincc.Action; | |
import com.github.pfmiles.dropincc.CC; | |
import com.github.pfmiles.dropincc.Exe; | |
import com.github.pfmiles.dropincc.Grule; | |
import com.github.pfmiles.dropincc.Lang; | |
import com.github.pfmiles.dropincc.TokenDef; | |
public class Test { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.pfmiles.dropincc.Action; | |
import com.github.pfmiles.dropincc.CC; | |
import com.github.pfmiles.dropincc.Exe; | |
import com.github.pfmiles.dropincc.Grule; | |
import com.github.pfmiles.dropincc.Lang; | |
import com.github.pfmiles.dropincc.TokenDef; | |
public class Calculator { | |
private static Exe exe = null; | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CloneableLazyList(object): | |
def __init__(self, gen): | |
self.gen = gen | |
self.cache = [] | |
self.index = 0 | |
def __iter__(self): | |
return self | |
def next(self): | |
if self.index < len(self.cache): | |
ret = self.cache[self.index] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
nid=`python -c "print hex($1)"` | |
grep -i $nid $2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
waterMark=10 | |
pid=`/usr/alibaba/java/bin/jps|grep AvatarEngine|cut -d ' ' -f 1` | |
curLoad=`cat /proc/loadavg |cut -d ' ' -f 1` | |
file="/tmp/$pid-`date +%Y-%m-%d-%H-%M-%S`-stack$curLoad.log" | |
if [ 1 == `echo "$curLoad > $waterMark"|bc` ];then | |
/usr/alibaba/java/bin/jstack $pid > $file | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import java.net.InetAddress; | |
import java.net.UnknownHostException; | |
import java.util.ArrayList; | |
import java.util.ConcurrentModificationException; | |
import java.util.List; | |
import java.util.Map; | |
public class GatewayUrgentDnsSwitchServiceImpl implements GatewayUrgentDnsSwitchService { |