问题 单项选择题

下列程序的输出结果为( )。
public class Reentrant

public synchronized void a()

b();
System.out.println("here I am, in a()");

public synchronized void b()

System.out.println("here I am, in b()");

public static void main(String args[ ])

Reentrant r=new Reentrant();
r.a();

A.here I am, in a()/here I am, in b()

B.hereI am, in b()/here I am, in a()

C.here I am, in a()

D.here I am, in b()

答案

参考答案:B

解析: 此题程序中类Reentrant定义了两个带有synchronized的方法,分别是a()和b()。在Reentrant类的main()方法中,Reentrant类的实例r调用了方法a(),在a()中调用b()。a()的执行过程中,线程的控制将请求并获得r的锁,并开始执行a()方法。由b()的定义可知,线程获得r的对象锁才能运行该方法,而此时r的锁已经由该线程获得,根据Java对象锁的可重入性,该线程将再次获得r的锁,并开始运行方法b()。

选择题
多项选择题