简体   繁体   中英

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.

So in Clojure I did this:

(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. In this case the first item is an integer, and Clojure doesn't treat integers as functions. If you remove the unnecessary parentheses, your var definition would look like this:

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

Notice how if you replace the method call with an integer, it becomes (def n-cpu 4) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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