Skip to content

Instantly share code, notes, and snippets.

@jonatasemidio
Created October 22, 2013 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonatasemidio/7100045 to your computer and use it in GitHub Desktop.
Save jonatasemidio/7100045 to your computer and use it in GitHub Desktop.
Concurrent Access Problems Threads share memory, and they can concurrently modify data. Since the modification can be done at the same time without safeguards, this can lead to unintuitive results. Data Races or Race condition or Race hazard When two or more threads are trying to access a variable and one of them wants to modify it, you get a pr…
//Let see with an example:
class Counter {
public static long count = 0;
}
class UseCounter implements Runnable {
public void increment() {
Counter.count++;
System.out.print(Counter.count + " ");
}
public void run() {
increment();
increment();
increment();
}
}
// This class creates three threads
public class DataRace {
public static void main(String args[]) {
UseCounter c = new UseCounter();
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
Thread t3 = new Thread(c);
t1.start();
t2.start();
t3.start();
}
}
//when you run this program, it does print nine integer values, but the output looks like
//3 3 5 6 3 7 8 4 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment