使用 JDK 16,我声明了两个注释:
@Target({ ElementType.RECORD_COMPONENT})
@Retention(RetentionPolicy.RUNTIME)
public @interface A {}
@Target({ ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface B {}
Run Code Online (Sandbox Code Playgroud)
我声明了一个像这样的记录类?
public record User(@A @B long id, String name, int age) {}
Run Code Online (Sandbox Code Playgroud)
然后我使用反射来获取id的注解,即:
Annotation[] annotations = fields[0].getAnnotations();
Run Code Online (Sandbox Code Playgroud)
但是 的大小annotations是 1 而我只有@B,这是为什么呢?谢谢
由于 Record 类实例没有公共 Object 实例所具有的 obj 头。但是我们仍然可以使用同步功能,例如:
public record User(long id, String name, int age) {}
User user = new User(1, "name", 18);
synchronized (user) {
user.wait();
System.out.println("test");
}
Run Code Online (Sandbox Code Playgroud)
从轻量级锁到重级锁的转换过程会不会像普通对象实例一样?