设为首页 加入收藏

TOP

并发遍历二叉树 Java 实现(二)
2014-11-24 07:15:06 来源: 作者: 【 】 浏览:5
Tags:并发 Java 实现
new Stack>();
while (node != null || !stack.isEmpty()) {
long start = System.currentTimeMillis();
while (node != null) {
//System.out.println("[ job" + id + " ]" + node.value);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}


stack.push(node);
node = node.left;
}
node = stack.pop();
node = node.right;


long cur = System.currentTimeMillis();
if (cur - start > 1000) {
if (handleNode(node)) {
node = null;
}
}
}
System.out.println("[ job" + id + " ] ---end");
tasks.remove(id);
if ( tasks.size() == 0) {
threadPool.shutdown();
}
}
}


public static void main(String[] args) {
ConcurTravBTree tt = new ConcurTravBTree();
System.out.println("init tree...");
tt.init();


System.out.println("start preOrderTraverse traverse...");
long start = System.currentTimeMillis();
tt.tree.preOrderTraverse();
long end = System.currentTimeMillis();
System.out.println("---- preOrderTraverse cost " + (end - start));

System.out.println("start concurrent traverse...");
long cstart = System.currentTimeMillis();
tt.start();
try {
tt.threadPool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
long cend = System.currentTimeMillis();
System.out.println("---- current traverse cost " + (cend - cstart));
}
}



首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Cocos2D 如何做 Android 适配 下一篇Struts2的入门实例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)