undefined

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Synchronization

Here's the new code snippet with a single keyword addition, synchronized, to fix the race condition:

class Task implements Runnable {
static int sum = 0;
synchronized public void run() { ++sum; }
}
public class Main {
public static void main(String args[]) throws Exception {
Task task = new Task();
for (int i = 0; i < 10000; i++) {
new Thread(task).start();
}
Thread.sleep(1000); // ad-hoc wait till threads finish
System.out.println("sum: " + Task.sum);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content