Last active
August 29, 2015 14:02
-
-
Save razie/681594e79f8c7ee76084 to your computer and use it in GitHub Desktop.
test volatile i++
This file contains 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
package razie.actionables; | |
/** | |
* result is false 69978 vs 100000 | |
* | |
* @author razvanc | |
* | |
*/ | |
public class TestVol { | |
volatile Long i = 0L; | |
static final int CYCLES = 10; | |
static final int THREADS = 200; | |
static final int TESTS = 20; | |
public void doit() { | |
for (int j = 0; j < CYCLES; j++) | |
i++; | |
} | |
public static void main(String[] argv) throws InterruptedException { | |
Thread[] threads2 = new Thread[THREADS]; | |
for (int tests = 0; tests < TESTS; tests++) { | |
final TestVol ii = new TestVol(); | |
for (int t = 0; t < THREADS; t++) | |
threads2[t] = new Thread() { | |
public void run() { | |
ii.doit(); | |
} | |
}; | |
for (int t = 0; t < THREADS; t++) | |
threads2[t].start(); | |
for (int t = 0; t < THREADS; t++) | |
threads2[t].join(); | |
System.out.println("" | |
+ (ii.i == CYCLES * THREADS * 1L ? "true" | |
: " false") + " " + ii.i + " vs " | |
+ CYCLES * THREADS * 1L); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment