Skip to content

Instantly share code, notes, and snippets.

@lichengwu
Created July 2, 2012 15:37
Show Gist options
  • Save lichengwu/3033833 to your computer and use it in GitHub Desktop.
Save lichengwu/3033833 to your computer and use it in GitHub Desktop.
volatile test
# method test1() i and j are not same
......
......
i=737354065j=737354127
i=737357077j=737357138
i=737362614j=737362697
i=737365141j=737365206
i=737368404j=737368461
i=737371663j=737371723
i=737374634j=737374694
i=737376637j=737376701
i=737380045j=737380103
i=737383164j=737383223
i=737386033j=737386096
i=737391217j=737391277
i=737394546j=737394605
i=737397796j=737397859
i=737400556j=737400619
i=737402600j=737402661
i=737406018j=737406079
i=737409278j=737409338
i=737412559j=737412559
i=737414224j=737414223
i=737415229j=737415287
i=737417126j=737417181
i=737418984j=737419045
i=737420830j=737420897
i=737422739j=737422796
i=737424671j=737424727
i=737426777j=737426835
i=737428684j=737428751
i=737430719j=737430826
i=737432771j=737432827
i=737434798j=737434854
i=737436742j=737436797
i=737438702j=737438758
i=737440608j=737440667
i=737442458j=737442515
i=737444397j=737444451
i=737446518j=737446572
i=737448416j=737448470
......
......
# method test2() i and j are same
......
......
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
i=45625514j=45625514
......
......
/*
* Copyright (c) 2010-2011 lichengwu
* All rights reserved.
*
*/
package oliver.test.sync;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
/**
*
* @author lichengwu
* @created 2012-7-2
*
* @version 1.0
*/
public class VolatileTest {
@Test
public void test1() throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
while (true)
WithOutIncrease.one();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true)
WithOutIncrease.two();
}
}).start();
TimeUnit.SECONDS.sleep(100000000);
}
@Test
public void test2() throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
while (true)
WithIncrease.one();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true)
WithIncrease.two();
}
}).start();
TimeUnit.SECONDS.sleep(100000000);
}
}
class WithOutIncrease {
static int i = 0;
static int j = 0;
public static void one() {
i++;
j++;
}
public static void two() {
System.out.println("i=" + i + "j=" + j);
}
}
class WithIncrease {
volatile static int i = 0;
volatile static int j = 0;
public static void one() {
i++;
j++;
}
public static void two() {
System.out.println("i=" + i + "j=" + j);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment