本文共 950 字,大约阅读时间需要 3 分钟。
是指一个操作是不可中断的。即使是多个线程一起执行的时候,一个操作一旦开始,就不会被其他线程干扰。原子操作一定是线程安全的。(32位机器读取64位的数据类型也不是原子操作)
并发程序,在执行的时候有可能会乱序,操作系统在执行指令的时候会进行一定的优化
指的是一个线程修改了共享内存的值,另一个线程是否感知的问题。
下面来看一看网上一个虚拟机层面的可见性问题
在server模式下面代码永远不会停止。
package com.ziwu.learn.thread;public class VisibilityTest extends Thread { private boolean stop; public boolean isStop() { return stop; } public void setStop(boolean stop) { this.stop = stop; } @Override public void run() { int i = 0; while(!stop) { i++; } System.out.println("finish loop,i=" + i); } public static void main(String[] args) throws InterruptedException { VisibilityTest test = new VisibilityTest(); test.start(); Thread.sleep(1000); test.setStop(true); Thread.sleep(2000); System.out.println(test.isStop()); }}
转载地址:http://ahazb.baihongyu.com/