如果synchronized锁住的对象被垃圾回收器回收了,会发生什么呢?
下面代码的目的是让线程t1先进入synchronized(a)代码块,然后主线程调用full gc回收对象a,然后看看发生了什么
public class Main {
//定义类A
public static class A{
int i = 3;
}
//等会要实验的静态引用a
public static A a = new A();
public static void main(String[] args) throws InterruptedException {
//实例化线程t1
Thread t1 = new Thread() {
@Override
public void run()
{
synchronized(a)
{
System.out.println("enter1");
//睡三秒好让主线程gc
try {
Thread.sleep(3000);
} catch (InterruptedException e) {