class Counter {
private int count = 0;
// Synchronized method to ensure thread safety
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
public class MultiThreadExample {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
// Define a task for multiple threads
Runnable task = () -> {
for (int i = 0; i < 1000; i++) counter.increment();
};
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start(); t2.start();
t1.join(); t2.join();
System.out.println("Final Count: " + counter.getCount()); // Should be 2000
}
}
class Counter {
private int count = 0;
// Synchronized method to ensure thread safety
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
public class MultiThreadExample {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
}