Learn R Programming

J4R (version 1.0.8)

mclapply.j4r: Using multithreading with J4R

Description

Applies the mclapply function in the context of the J4R package.

Usage

mclapply.j4r(X, FUN, ..., nbCores = getNbConnections())

Arguments

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.

Details

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.

See Also

mclapply in the parallel package

getNbConnections

Examples

Run this code
# NOT RUN {
f <- function(i, aff) {
   myArrayList <- createJavaObject("java.util.ArrayList", affinity = aff)
   myArrayList$add(5, affinity = aff)
}

result <- mclapply.j4r(1:1000, f)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab