dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
}
import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.Dispatchers;
import kotlinx.coroutines.launch;
public class Main {
public static void main(String[] args) {
CoroutineScope scope = CoroutineScope(Dispatchers.Default);
scope.launch(() -> {
// ...
});
}
}
import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.Dispatchers;
import kotlinx.coroutines.launch;
public class Main {
public static void main(String[] args) {
CoroutineScope scope = CoroutineScope(Dispatchers.IO);
scope.launch(() -> {
// ...
});
}
}
import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.Dispatchers;
import kotlinx.coroutines.async;
import kotlinx.coroutines.awaitAll;
public class Main {
public static void main(String[] args) {
CoroutineScope scope = CoroutineScope(Dispatchers.Default);
scope.launch(() -> {
List<Deferred<Integer>> deferredList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
deferredList.add(async(() -> {
// ...
return result;
}));
}
List<Integer> results = awaitAll(deferredList.toArray(new Deferred[0]));
for (int result : results) {
// ...
}
});
}
}