如何异步调用一个方法并 运行 它?
How to call a method and run it asynchronously?
有没有办法在 Java 8 中仅从 main()
异步 运行 getUsers()
?
运行 getUsers()
在主线程上需要 300 秒。我希望用 4 个核心在 180 秒内完成。
public class Main {
// Getting users (with sleep)
public List<User> getUsers() {
List<User> users = new ArrayList();
for(int i = 0; i < 100; i++) {
users.add(new User("someName", "someAge"));
TimeUnit.MILLISECONDS.sleep(3000);
}
return users;
}
public static void main(String[] args) {
List<User> users = getUsers();
}
}
在不修改 getUsers()
(objective)的情况下,运行以下内容仍需要 300 秒:
public static void main(String[] args) {
List<User> users =
CompletableFuture.supplyAsync(() -> getUsers()).join();
}
您可以将用户创建循环分成更小的循环,然后 运行 它们并行进行。考虑这个例子,运行s 快 4 倍:
public static void main(String[] args) {
// create a pool executor of 4 threads, increase or decrease it if need
Executor executor = Executors.newFixedThreadPool(4);
CompletableFuture<List<User>> future1 = CompletableFuture.supplyAsync(() -> getUsers(25), executor);
CompletableFuture<List<User>> future2 = CompletableFuture.supplyAsync(() -> getUsers(25), executor);
CompletableFuture<List<User>> future3 = CompletableFuture.supplyAsync(() -> getUsers(25), executor);
CompletableFuture<List<User>> future4 = CompletableFuture.supplyAsync(() -> getUsers(25), executor);
List<User> allUsers = Stream.of(future1, future2, future3, future4)
.map(CompletableFuture::join)
.flatMap(List::stream)
.collect(Collectors.toList());
}
public static List<User> getUsers(int count) {
List<User> users = new ArrayList<>();
for (int i = 0; i < count; i++) {
users.add(new User("someName", "someAge"));
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return users;
}
有没有办法在 Java 8 中仅从 main()
异步 运行 getUsers()
?
运行 getUsers()
在主线程上需要 300 秒。我希望用 4 个核心在 180 秒内完成。
public class Main {
// Getting users (with sleep)
public List<User> getUsers() {
List<User> users = new ArrayList();
for(int i = 0; i < 100; i++) {
users.add(new User("someName", "someAge"));
TimeUnit.MILLISECONDS.sleep(3000);
}
return users;
}
public static void main(String[] args) {
List<User> users = getUsers();
}
}
在不修改 getUsers()
(objective)的情况下,运行以下内容仍需要 300 秒:
public static void main(String[] args) {
List<User> users =
CompletableFuture.supplyAsync(() -> getUsers()).join();
}
您可以将用户创建循环分成更小的循环,然后 运行 它们并行进行。考虑这个例子,运行s 快 4 倍:
public static void main(String[] args) {
// create a pool executor of 4 threads, increase or decrease it if need
Executor executor = Executors.newFixedThreadPool(4);
CompletableFuture<List<User>> future1 = CompletableFuture.supplyAsync(() -> getUsers(25), executor);
CompletableFuture<List<User>> future2 = CompletableFuture.supplyAsync(() -> getUsers(25), executor);
CompletableFuture<List<User>> future3 = CompletableFuture.supplyAsync(() -> getUsers(25), executor);
CompletableFuture<List<User>> future4 = CompletableFuture.supplyAsync(() -> getUsers(25), executor);
List<User> allUsers = Stream.of(future1, future2, future3, future4)
.map(CompletableFuture::join)
.flatMap(List::stream)
.collect(Collectors.toList());
}
public static List<User> getUsers(int count) {
List<User> users = new ArrayList<>();
for (int i = 0; i < count; i++) {
users.add(new User("someName", "someAge"));
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return users;
}