Skip to content

refactor: improved examples for CopyOnWriteArrayList #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,42 @@ public synchronized void addIfAbsent(String element) {
public int size() {
return list.size();
}
}

public static void main(String[] args) {
CorrectUsage usage = new CorrectUsage();

// Create threads to perform operations on the list
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 100; i++) {
usage.addIfAbsent("Element " + i);
}
});

Thread thread2 = new Thread(() -> {
for (int i = 50; i < 150; i++) {
usage.addIfAbsent("Element " + i);
}
});

// Start the threads
thread1.start();
thread2.start();

// Wait for both threads to complete
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

// Print the final size of the list
System.out.println("Final size of the list: " + usage.size());

// Print elements in the list to demonstrate correct usage
System.out.println("Elements in the list:");
for (String element : usage.list) {
System.out.println(element);
}
}
}