Skip to content

Instantly share code, notes, and snippets.

@0minus273
Created April 10, 2014 10:40
Show Gist options
  • Save 0minus273/10366856 to your computer and use it in GitHub Desktop.
Save 0minus273/10366856 to your computer and use it in GitHub Desktop.
eh?
public class myE {
static int counter = 0;
public static void main(String[] args) throws InterruptedException {
if (args.length < 1) {
throw new IllegalArgumentException("Usage: Ex1q9 N");
}
Integer numberOfThreads = Integer.valueOf(args[0]);
Thread[] threads = new Thread[numberOfThreads];
long start = System.currentTimeMillis();
for (int i = 0; i < numberOfThreads; i++) {
threads[i] = new Thread(new Runnable() {
//@Override
public void run() {
for (int j = 0; j < 10000; j++) {
int localCounter = counter;
localCounter += 1;
counter = localCounter;
}
}
});
threads[i].start();
}
for (Thread thread : threads) {
thread.join();
}
long duration = System.currentTimeMillis() - start;
System.out.println(String.format("counter=%d duration=%d", counter, duration));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment