Created
April 10, 2014 10:40
-
-
Save 0minus273/10366856 to your computer and use it in GitHub Desktop.
eh?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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