【问题标题】:Making a Tasks Tree制作任务树
【发布时间】:2014-09-16 19:14:11
【问题描述】:

我是否可以创建一个包含字符串的对象树。比如如下图

         T1
         |
         T2
         /\
        T3 T4
         \ /
          T5

创建这样的树的语法是什么?我正在努力使每个都按顺序、并行和嵌套顺序运行。

这是我迄今为止所写的:

    public class Tasks {
    public Node node;
    public String put(String value, int[] path){

    Node current = node;

    for(int i=0;i<path.length;i++){
        if(current.children.get(i)==null){
            current.children.add(i, new Node());
        }
        current = current.children.get(i);
    }
    String ret = current.value;
    current.value = value;

   Tasks myTask = new Tasks();
    myTask.node = new Node();
    int[] path1 = {0, 0, 0};
    myTask.put("T1", path1);
    System.out.println(myTask.get(path1));
}

 }

public class Node{
public ArrayList<Node> children;
public Node parent;
public String value;

}

【问题讨论】:

  • 您到底遇到了什么问题?怎么上课?如何实例化它们并相互引用?
  • 从技术上讲,您绘制的不是树。可以使用类似于树的符号来表示它。
  • 对我来说感觉就像XY problem。你为什么要创建这个结构?您需要存储哪些对象?你说你正在努力让each run on sequence, parallel and nesting order - 每个什么?查看help center 以获取有关编写更好问题的提示。您提供的信息越多,您将获得的帮助就越多。
  • 如果我听起来含糊不清,我深表歉意。我要做的是让每个任务以预定的方式执行某种计算,并发,并行,最后嵌套。字符串是每个任务将持有的内容。 “如何实例化它们并相互引用?” 是的!.....
  • 我可以想象一些执行者的解决方案。但首先:你已经有一些代码可以开始了吗?

标签: java tree scheduled-tasks distributed


【解决方案1】:

请查看此示例,该示例使用根据您给定的树结构组织的 ExecutorServiceCallable 任务。它已准备好运行,因此您可以对其进行测试和试验:

public class Test {

    public static void main(String[] args) throws Exception {
        // Declare a new ExecutorService with a maximum of 2 threads.
        ExecutorService service = Executors.newFixedThreadPool(2);

        // Declare the tasks.
        StringTask t1 = new StringTask("First task!");
        StringTask t2 = new StringTask("Second task!");
        StringTask t3 = new StringTask("Third task!");
        StringTask t4 = new StringTask("Fourth task!");
        StringTask t5 = new StringTask("Fifth task!");

        // Submit the first task.
        String result1 = service.submit(t1).get(); // Using the .get() directly blocks the waiting for completion of the task.
        System.out.println("T1 result: " + result1);

        // Submit the second task.
        String result2 = service.submit(t2).get();
        System.out.println("T2 result: " + result2);

        // Prepare the list of parallel tasks and invoke them.
        List<StringTask> parallelTasks = new ArrayList<>(2);
        parallelTasks.add(t3);
        parallelTasks.add(t4);
        List<Future<String>> results = service.invokeAll(parallelTasks); // Note that using .invokeAll() will block the
                                                                         // execution until all tasks are completed.
        for (Future<String> result : results) {
            System.out.println("Parallel task result: " + result.get());
        }

        // Submit the last task.
        String result5 = service.submit(t5).get();
        System.out.println("T5 result: " + result5);
    }

    /**
     * Callable task that reverses a given String.
     */
    private static final class StringTask implements Callable<String> {
        private String input;

        private StringTask(String input) {
            super();

            if (input == null) {
                throw new NullPointerException();
            }

            this.input = input;
        }

        @Override
        public String call() throws Exception {
            StringBuilder builder = new StringBuilder();

            for (int i = this.input.length() - 1; i >= 0; i--) {
                builder.append(this.input.charAt(i));
            }

            return builder.toString();
        }
    }
}

如果您事先知道任务以及它们的组织方式,您可以以类似的方式硬编码它们。但是,如果任务结构不是静态的或事先不知道,您可能希望实现自己的任务树结构(例如使用CallableNode 对象)并使用ExecutorService 遍历该树并根据到它的结构。

【讨论】:

  • 谢谢!我已经开始使用这种方法。我会尽快更新你的!
  • @Christy 不要忘记您可以调整实现Callable 的类(甚至创建不同的类)以更好地满足您的需求。我的只是一个简单的例子!
猜你喜欢
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2011-04-21
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
相关资源
最近更新 更多