Asynchronous email sending

Report a typo

Some operations, like email sending, are often performed in a background thread. It allows reducing the load for main threads.

Here is the MailSender interface and the MockMailSender class, which simulate mail sending.

Implement the asyncSend method by iterating through messages and sending them via sender. Use a single thread pool executor and do not forget to shut it down.

Write a program in Java 17
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Main {
// Do not change it
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int messageCounts = scanner.nextInt();
List<String> messages = new ArrayList<>();
for (int i = 0; i < messageCounts; ++i) {
messages.add(scanner.next());
}

MailSender sender = new MockMailSender();
asyncSend(sender, messages);
}

static void asyncSend(MailSender sender, List<String> messages) {
// TODO
}
}

// Do not change it
interface MailSender {
void send(String message);
}

// The class simulates mail sending
// Do not change it
class MockMailSender implements MailSender {
public void send(String message) {
System.out.println("Message " + message + " was sent");
}
___

Create a free account to access the full topic