java program3
java program3
Votes are cast for candidates by their names. Multiple threads simulate voting by
casting votes for various candidates concurrently.
Each vote involves a small artificial delay(Thread.sleep(3000) to simulate the time
it takes for network requests or database operations to complete. This increases
the likelihood of concurrent access issues.
You need to track the total votes for each candidate in a thread-safe manner.
After all threads finish, display the candidates sorted by the number of votes
received in descending order.
import java.util.*;
import java.util.concurrent.*;
// List of candidates
private static final List<String> candidates = Arrays.asList("Alice", "Bob",
"Charlie", "David");
@Override
public void run() {
try {
// Simulate network delay
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
output:
Voting Results:
Bob: 29 votes
David: 29 votes
Alice: 22 votes
Charlie: 20 votes