mclapply.j4r: Using multithreading with J4R

Description Usage Arguments Details See Also Examples

View source: R/j4rFunctions.R

Description

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

Usage

1
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

1
2
3
4
5
6
7
8
9
## 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)

J4R documentation built on July 23, 2020, 9:06 a.m.