有多少个可调用对象?

时间:2019-12-18 12:11:00

标签: java multithreading callable

有人可以帮我解决这个问题吗?

为您提供了一个Callable对象。有人说它返回另一个Callable对象。然后该Callable返回另一个Callable对象!等等。您应该找出有多少可调用对象。编写解决此问题的方法。

我不知道“最后”可赎回收益是什么,也许不是“空”。


    public static int determineCallableDepth(Callable callable) {
        return countCallable(0, callable);
    }

    public static int countCallable(int countCallable, Callable callToCount) {
        if (callToCount == null) {
            return countCallable;
        }
        try {
            return countCallable(countCallable + 1, (Callable)callToCount.call());
        } catch (Exception e) {
            e.printStackTrace();
            return countCallable;
        }
    }
}```

1 个答案:

答案 0 :(得分:0)

这是我完成此任务的代码。

public static int determineCallableDepth(Callable callable) {
    Object temp = null;
    try { 
        temp = callable.call();
    } catch (Exception e) {}
    return temp instanceof Callable ? 1 + determineCallableDepth((Callable) temp) : 1;
}

我认为您不需要任何说明:D