mclapply.j4r | R Documentation |
Applies the mclapply function in the context of the J4R package.
mclapply.j4r(X, FUN, ..., nbCores = getNbConnections())
X |
a vector of numerics |
FUN |
a two-argument function. The first argument is called by the mclapply function and the second argument defines the affinity and MUST be used in all the calls to the createJavaObject, callJavaMethod, getJavaField and setJavaField functions. |
... |
optional arguments to FUN (see mclapply) |
nbCores |
the number of threads to be used. By default, this argument is set to the number of available connections. |
Multithreading a function requires that the Java code is thread safe. The server must listen to at least two ports. Otherwise, this function will reduce to a single thread. Each port is given an affinity to an R thread.
The multithreading is not available on Windows. In such a case, the function will proceed in a single thread. The $ operator should not be used to substitute the getJavaField and setJavaField functions because it does not allow for the specification of the affinity. Use the original getJavaField and setJavaField functions. The $ operator can be used to call functions though as in the example below.
mclapply in the parallel package
getNbConnections
## Not run:
f <- function(i, aff) {
myArrayList <- createJavaObject("java.util.ArrayList", affinity = aff)
myArrayList$add(5, affinity = aff)
}
result <- mclapply.j4r(1:1000, f)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.