我有一个二叉树,其中每个节点代表一个电子门(和,OR,.)。我的任务是计算树的总价值(就像图片中的这个二叉树):

到目前为止,这是我的代码(没有线程实现):
gate_node:
public class gate_node {
gate_node right_c, left_c;
Oprtator op;
int value;
int right_v, left_v;
public gate_node(gate_node right, gate_node left, Oprtator op) {
this.left_c = left;
this.right_c = right;
this.op = op;
right_v = left_v = 0;
}
void add_input(int right_v, int left_v){
this.right_v=right_v;
this.left_v=left_v;
}
int compute(int array_index, int arr_size) {
/*
* The following use of a static sInputCounter assumes that the
* static/global input array is ordered from left to right, irrespective
* of "depth".
*/
final int left, right;
System.out.print(this.op+"(");
if (null != this.left_c) {
left = this.left_c.compute(array_index,arr_size/2);
System.out.print(",");
} else {
left = main_class.arr[array_index];
System.out.print(left + ",");
}
if (null != this.right_c) {
right = this.right_c.compute(array_index + arr_size/2,arr_size/2);
System.out.print(")");
} else {
right = main_class.arr[array_index + 1];
System.out.print(right + ")");
}
return op.calc(left, right);
}
}预报员:
public abstract class Oprtator {
abstract int calc(int x, int y);
}和
public class and extends Oprtator {
public int calc(int x, int y){
return (x&y);
}
}或
public class or extends Oprtator {
public int calc(int x, int y){
return (x|y);
}
}树:
public class tree implements Runnable {
gate_node head;
tree(gate_node head) {
this.head = head;
}
void go_right() {
head = head.right_c;
}
void go_left() {
head = head.left_c;
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}主班
public class main_class {
public static int arr[] = { 1, 1, 0, 1, 0, 1, 0, 1 };
public static void main(String[] args) {
tree t = new tree(new gate_node(null, null, new and()));
t.head.right_c = new gate_node(null, null, new or());
t.head.right_c.right_c = new gate_node(null, null, new and());
t.head.right_c.left_c = new gate_node(null, null, new and());
t.head.left_c = new gate_node(null, null, new or());
t.head.left_c.right_c = new gate_node(null, null, new and());
t.head.left_c.left_c = new gate_node(null, null, new and());
int res = t.head.compute(0, arr.length);
System.out.println();
System.out.println("The result is: " + res);
}
}我想使用线程池来计算它,就像这样的算法:
制备:
循环:
唯一的问题是,我不知道如何创建共享的BlockingQueue,树中的每个节点对象都可以将自己插入其中,以及如何创建一个由固定大小的线程组成的数组,不断等待队列中的新元素可用(然后执行它们)……直到将头从列表中删除(这意味着我们已经计算完毕)。
我在网上搜索了BlockingQueue示例,但我只找到了生产者和消费者示例,而且我很难移动这些示例以适应我的问题。如果有人能帮我,我会很感激的。
发布于 2013-12-20 18:25:24
我可以给你一些开始的指点,让你开始:)
要创建线程,只需生成以下多个线程:
for (int i=0;i<MAX_THREADS;i++) {
new Thread(myRunnable).start();
}您很可能希望存储对这些线程的引用,但它不是必需的。线程不需要特殊的设置,因为它们都是相同的,它们都只是坐在那里抓取队列中的项目。
要共享阻塞队列,最简单的方法就是使其成为静态和最终的:
static final BlockingQueue blockingQueue();现在所有线程都可以访问它。
顺便说一句,如果我这样做,我根本不会使用队列,我会使用一个ThreadPoolExecutor,只需将处理作为新的可运行项发送到该队列。
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
https://stackoverflow.com/questions/20709660
复制相似问题