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.
CopyOnWriteArrayList
Print element
Report a typo
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:
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.