Print element

Report a typo

Your task is to finish two methods: addNumbers and removeNumbers. The addNumbers method must add numbers from 0 to 100_000 to onWriteArrayList (inclusive). The removeNumbers method must remove all numbers from 0 to 50_000 (inclusive) from onWriteArrayList.

Write a program in Java 17
import java.util.concurrent.CopyOnWriteArrayList;

class UseCopyOnWriteArrayList {

public static void printElement(int n) throws InterruptedException {

CopyOnWriteArrayList<Integer> onWriteArrayList = new CopyOnWriteArrayList<>();

Thread writer1 = new Thread(() -> Main.addNumbers(onWriteArrayList));
Thread writer2 = new Thread(() -> Main.removeNumbers(onWriteArrayList));

writer1.start();
writer2.start();

writer1.join();
writer2.join();

System.out.println(onWriteArrayList.get(n));
}
}

public class Main {

public static void main(String[] args) throws InterruptedException {

Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();

UseCopyOnWriteArrayList.printElement(n);
}

public static void addNumbers(List<Integer> list) {
// write your code here:

}

___

Create a free account to access the full topic