文章目录
-
- 例如,文件所在目录在此处 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); } } View Code
- javap -c Student 报错提示: 找不到类 文末查看解决方法
研究synchronized底层实现,涉及到查看java字节码的需要
前提是,你的PC已经成功安装了JDK并别配置了环境变量。
==========查看方法=========
例如,文件所在目录在此处
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); } }
View Code
javap -c Student
报错提示:
找不到类
文末查看解决方法
javap -verbose Student
==========报错解决=========
找不到类
先执行一次javac命令,在此处生成class文件后,再执行javap命令即可。
如下:
先执行
javac Student.java
再执行
javap -c Student







发表回复