研究synchronized底层实现,涉及到查看java字节码的需要
前提是,你的PC已经成功安装了JDK并别配置了环境变量。
==========查看方法=========
1.准备一个java文件
例如,文件所在目录在此处
Student.java文件内容如下:
package com.sxd.sweeping.test.synchron; public class Student implements Runnable{ static int age; public static synchronized void add(){ age++; } @Override public void run() { int size = 100000; for (int i = 0; i < size; i++) { add(); } } public static void main(String[] args) { Thread thread1 = new Thread(new Student()); Thread thread2 = new Thread(new Student()); thread1.start(); thread2.start(); try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Student.age); } }