Skip to content

refactor: improved examples for ExcessiveSyncCounter, OptimizedCounter #54

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 29, 2024
Merged
Show file tree
Hide file tree
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,4 +35,52 @@ public synchronized void decrement() {
public synchronized int getCount() {
return count;
}

public static void main(String[] args) {
ExcessiveSyncCounter counter = new ExcessiveSyncCounter();

// Create threads to perform operations on the counter
Thread incrementThread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

Thread incrementThread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

Thread decrementThread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.decrement();
}
});

Thread decrementThread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.decrement();
}
});

// Start the threads
incrementThread1.start();
incrementThread2.start();
decrementThread1.start();
decrementThread2.start();

// Wait for all threads to complete
try {
incrementThread1.join();
incrementThread2.join();
decrementThread1.join();
decrementThread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

// Print the final count
System.out.println("Final count: " + counter.getCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,53 @@ public int getCount() {
return count;
}
}

public static void main(String[] args) {
OptimizedCounter counter = new OptimizedCounter();

// Create threads to perform operations on the counter
Thread incrementThread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

Thread incrementThread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

Thread decrementThread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.decrement();
}
});

Thread decrementThread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.decrement();
}
});

// Start the threads
incrementThread1.start();
incrementThread2.start();
decrementThread1.start();
decrementThread2.start();

// Wait for all threads to complete
try {
incrementThread1.join();
incrementThread2.join();
decrementThread1.join();
decrementThread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

// Print the final count
System.out.println("Final count: " + counter.getCount());
}
}