爱问知识人 爱问教育 医院库

Java Thread的语法和编译结果是什么?

首页

Java Thread的语法和编译结果是什么?

Java Thread的语法和编译结果是什么?

提交回答

全部答案

    2017-07-28 09:57:05
  •   用synchronized关键字修饰的方法,分为两种情况:(static)静态方法,和实例方法。(static)静态方法的“锁”是这个拥有这个方法的对象的Class对象;实例方法的“锁”是this,拥有这个方法的当前对象实例。怎么理解这段话,看一看下面的例子就明白了。
      下面两段代码的效果完全相同。代码1 ==代码2。代码1:class Test {int count;synchronized void bump() { count++; }static int classCount;static synchronized void classBump() {classCount++;}}代码2:class BumpTest {int count;void bump() {synchronized (this) {count++;}}static int classCount;static void classBump() {try {synchronized (Class。
      forName("BumpTest")) {classCount++;}} catch (ClassNotFoundException e) {。。。}}}2.synchronized关键字的编译结果这一节,我们来看一看synchronized关键字编译之后的java虚拟机指令是什么。
      如果需要第一手资料,请参见java虚拟机规范相关的部分http://java。sun。com/docs/books/vmspec/2nd-edition/html/Compiling。doc。html#6530这段规范里面讲到,java虚拟机规范提供两条指令,monitorenter和monitorexit,来支持线程。
      但是对于上一节讲到的,用synchronized修饰的方法来说,并不使用这两个方法,而只是简单地用ACC_SYNCHRONIZED标志修饰。虚拟机调用方法的时候会检查这个标志,进行同步。synchronized语句的编译结果对应monitorenter和monitorexit两条指令。
      比如,下面的代码:void onlyMe(Foo f) {synchronized(f) {doSomething();}}的编译结果是Method void onlyMe(Foo)0 aload_1 // Push f 1 astore_2 // Store it in local variable 22 aload_2 // Push local variable 2 (f)3 monitorenter // Enter the monitor associated with f4 aload_0 // Holding the monitor, pass this and。
      。。5 invokevirtual #5 // 。。。call Example。doSomething()V8 aload_2 // Push local variable 2 (f)9 monitorexit // Exit the monitor associated with f10 return // Return normally11 aload_2 // In case of any throw, end up here12 monitorexit // Be sure to exit monitor。
      。。13 athrow // 。。。then rethrow the value to the invoker3.monitorenter和monitorexit详见http://java。sun。com/docs/books/vmspec/2nd-edition/html/Instructions2。
      doc9。htmlmonitorenter定义的一段节录:Operation : Enter monitor for objectOperand Stack : 。。。, objectref 。。。 Description :The objectref must be of type reference。
      Each object has a monitor associated with it。 The thread that executes monitorenter gains ownership of the monitor associated with objectref。
       If another thread already owns the monitor associated with objectref, the current thread waits until the object is unlocked, then tries again to gain ownership。
       If the current thread already owns the monitor associated with objectref, it increments a counter in the monitor indicating the number of times this thread has entered the monitor。
       If the monitor associated with objectref is not owned by any thread, the current thread becomes the owner of the monitor, setting the entry count of this monitor to 1。
      这段话的意思是说,monitorenter操作的目标一定要是一个对象,类型是reference。Reference实际就是堆里的一个存放对象的地址。每个对象(reference)都有一个monitor对应,如果有其它的线程获取了这个对象的monitor,当前的线程就要一直等待,直到获得monitor的线程放弃monitor,当前的线程才有机会获得monitor。
      如果monitor没有被任何线程获取,那么当前线程获取这个monitor,把monitor的entry count设置为1。表示这个monitor被1个线程占用了。当前线程获取了monitor之后,会增加这个monitor的时间计数,来记录当前线程占用了monitor多长时间。
      我们看到,monitor这个词在java虚拟机规范规定出现,但是在java语言和API文档里面并没有出现。monitor是藏在线程同步后面的原理和概念。

    重***

    2017-07-28 09:57:05

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):