简体   繁体   English

Clojure 中的处理器数量? Java互操作

[英]Number of Processors in Clojure? Java interop

I'd like to use the Java method: Runtime.getRuntime().availableProcessors() and save the result in a integer variable.我想使用 Java 方法: Runtime.getRuntime().availableProcessors()并将结果保存在一个整数变量中。

So in Clojure I did this:所以在 Clojure 我做了这个:

(def n-cpu  ((.availableProcessors (Runtime/getRuntime)) ))

and this:和这个:

(def n-cpu (Integer/parseInt ((.availableProcessors (Runtime/getRuntime)) )))

but none work.但没有工作。

Any suggestions?有什么建议?

If you replace the method call in your version with an integer, this is what you logically have:如果你用一个整数替换你版本中的方法调用,这就是你逻辑上的:

(def n-cpu (4))

Clojure cannot handle the list (4) because the first item in a non-quoted list must be a function. Clojure 无法处理列表(4)因为非引用列表中的第一项必须是函数。 In this case the first item is an integer, and Clojure doesn't treat integers as functions.在这种情况下,第一项是整数,Clojure 不会将整数视为函数。 If you remove the unnecessary parentheses, your var definition would look like this:如果删除不必要的括号,您的 var 定义将如下所示:

(def n-cpu (.availableProcessors (Runtime/getRuntime)))

Notice how if you replace the method call with an integer, it becomes (def n-cpu 4) ?请注意,如果用整数替换方法调用,它会变成(def n-cpu 4)吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM