zookeeper

int consume() throws KeeperException, InterruptedException{
    int retvalue = -1;
    Stat stat = null;
    while (true) {
        synchronized (mutex) {
             List<String> list = zk.getChildren(root, true);
             if (list.size() == 0) {
                 mutex.wait();
             } else {
                 Integer min = new Integer(list.get(0).substring(7));
                 for(String s : list){
                     Integer tempValue = new Integer(s.substring(7));
                     if(tempValue < min) min = tempValue;
                 }
                 byte[] b = zk.getData(root + "/element" + min,false, stat);
                 zk.delete(root + "/element" + min, 0);
                 ByteBuffer buffer = ByteBuffer.wrap(b);
                 retvalue = buffer.getInt();
                 return retvalue;
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章