volatile的作用,相信大家已经不陌生了,总体来说,有两个作用:
- 有序性:禁止代码重排序。这个在单例模式中用的比较多
- 可见性:多个线程访问同一个变量时,这个变量被修改后,能被其他的线程看到。这个在多线程访问同一个变量时比较常用。
一直以来,是这样记得,但没有写代码验证过,今天写了一下代码,验证一下:
目的,主线程修改某了变量,如果子线程能正确识别到了更改,子线程可以正常结束
1. 普通读取,主线程先于子线程运行
public class ThreadTest {
static int a = 0;
public static void main(String[] args) {
new Thread("Test") {
@Override
public void run() {
System.out.println("Child Thread come in ...");
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
while (a == 0) {
}
System.out.println("Child Thread end");
}
}.start();
a++;
System.out.println("Main Thread End" + "a=" + a);
}
}
结果:
Main Thread Enda=1
Child Thread come in ...
Child Thread end
子线程可以正常结束,原因主线程先运行了a++操作后,子线程才开始运行的,所以子线程中拿到的是最新的值。
2. 普通读取,主线程后于子线程运行
public class ThreadTest {
static int a = 0;
public static void main(String[] args) {
new Thread("Test") {
@Override
public void run() {
System.out.println("Child Thread come in ...");
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
while (a == 0) {
}
System.out.println("Child Thread end");
}
}.start();
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
a++;
System.out.println("Main Thread End" + "a=" + a);
}
}
结果:
Child Thread come in ...
Main Thread Enda=1
子线程没有正常结束,原因子线程先进行,读取了a的值,当主线程修改后,子线程无法得知,故子线程无法正常结束。
3. volatile读取,主线程后于子线程运行
public class ThreadTest {
static volatile int a = 0;
public static void main(String[] args) {
new Thread("Test") {
@Override
public void run() {
System.out.println("Child Thread come in ...");
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
while (a == 0) {
}
System.out.println("Child Thread end");
}
}.start();
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
a++;
System.out.println("Main Thread End" + "a=" + a);
}
}
结果:
Child Thread come in ...
Child Thread end
Main Thread Enda=1
子线程可以正常结束,使用了关键字volatile,当主线程更新了a的值后,子线程可以得知,进而正常结束
4.普通读取,子线程先于主线程执行,并增加同步锁<1>
public class ThreadTest {
static int a = 0;
static Object lock = new Object();
public static void main(String[] args) {
new Thread("Test") {
@Override
public void run() {
System.out.println("Child Thread come in ...");
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
while (a == 0) {
synchronized (lock) {
}
}
System.out.println("Child Thread end");
}
}.start();
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
a++;
System.out.println("Main Thread End" + "a=" + a);
}
}
结果:
Child Thread come in ...
Child Thread end
Main Thread Enda=1
子线程可以正常结束,原因是因为synchronized起到了作用,synchronized在运行时,会清空工作内存数据,并从主内存拷贝对象副本到工作内存再执行相应代码,因此子线程可以得到最新的数据,进而可以正常结束。同理synchronized(this)也是上述的效果。
5.普通读取,子线程先于主线程执行,并增加同步锁<2>
public class ThreadTest {
static int a = 0;
public static void main(String[] args) {
new Thread("Test") {
@Override
public void run() {
System.out.println("Child Thread come in ...");
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
while (a == 0) {
synchronized (new Object()) {
}
}
System.out.println("Child Thread end");
}
}.start();
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
a++;
System.out.println("Main Thread End" + "a=" + a);
}
}
结果:
Child Thread come in ...
Main Thread Enda=1
子线程无法正常结束,虽然也用到了同步及锁,但每次都new Object(),这个锁根本无法被竞争,所以起不到同步的作用,也就不会执行同步的相关操作,这相当于没有同步,所以无法结束子线程。
6.普通读取,子线程先于主线程执行,增加打印println
public class ThreadTest {
static int a = 0;
public static void main(String[] args) {
new Thread("Test") {
@Override
public void run() {
System.out.println("Child Thread come in ...");
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
while (a == 0) {
System.out.println("Child Thread a=" + a);
}
System.out.println("Child Thread end");
}
}.start();
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
a++;
System.out.println("Main Thread End" + "a=" + a);
}
}
结果:
Child Thread come in ...
Child Thread a=0
Child Thread a=0
Child Thread a=0
Child Thread a=0
...
Child Thread a=0
Child Thread end
Main Thread Enda=1
子线程可以正常结束,这个打印是我无意中加入的,当时挺纳闷的,怎么增加了打印后,就能同步了,后来查看print源码才发现原因:
public void println(String var1) {
synchronized(this) {
this.print(var1);
this.newLine();
}
}
在print方法中,有同步代码块,所以会进行相关的同步操作,进而使子线程拿到最新的值,正常结束。
网友评论