GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java-Interview-Advanced/docs/03/69.md at master · leelovejava/Java-Interview-Advanced

Expand file tree

/

69.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
30 lines (10 loc) · 972 Bytes
/

69.md

Copy path

File metadata and controls

30 lines (10 loc) · 972 Bytes
Raw
Copy raw file
Download raw file
Edit and raw actions

69、volatile原来还可以保证long和double变量写操作的原子性

volatile对原子性保障的语义,在java里很有限的,几乎可以忽略不计。32位的java虚拟机里面,对long/double变量的赋值写是不原子的,此时如果对变量加上了volatile,就可以保证在32位java虚拟机里面,对long/double变量的赋值写是原子的了

int i = 0,原子性,volatile,java语言规范就规定了,原子性的

volatile long i;

多个线程执行:i = 30,此时就不要紧了,因为volatile修饰了,就可以保证这个赋值操作是原子的了

你以后出去面试也可能会遇到杠精面试官,你要说volatile是保证可见性和有序性的,不保证原子性,杠精面试官,素质差,很二,心胸很狭隘,volatile可以保证原子性,此时看过这一讲之后

i++,复杂的一些场景

resources = loadResources();

resources.execute();

ready = true;

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.