JVM对synchronized的优化——锁膨胀

并发编程----5、锁膨胀 锁膨胀就是所谓的 锁从 无锁-> 偏向锁 -> 轻量锁 -> 重量锁 的过程。锁膨胀没什么太多细节需要讲,主要说下上一讲中的一些疑问。再讲一下批量冲偏向和批量撤销的原理。 目录 一、前情回顾 二、批量重偏向 三、批量撤销 一、前情回顾 我们看下面代码 package com.hubin.lock; import org.openjdk.jol.info.ClassLayout; import static java.lang.System.out; public cl. 阅读详情

JVM对synchronized的优化——锁膨胀

前言

  • 通常我们在synchronized(…)中传一个对象,即可实现加锁,非常简单。而使用锁,常见的问题就是效率慢。在较早期的Java中synchronized会直接调用系统的重量级互斥锁(monitor)来实现加锁,效率较慢。锁膨胀则是针对该问题的优化方案:先由JVM自己管理锁,如果不行才调用系统的重量级锁(由无锁升级为偏向锁,再升级为轻量级锁,最后升级为重量级锁)。

synchronized字节码操作

  • synchronized代码示例
    public class Demo01 {
    
        public static void main(String[] args) {
            synchronized ("lockObj") {
                System.out.println("Demo01.main");
            }
        }
    
    }
    
  • 执行 javap -c .\Demo01.class,反编译生成字节码操作
  • main方法的字节码操作
    public static void main(java.lang.String[]);
        Code:
           0: ldc           #2                  // String lockObj
           2: dup
           3: astore_1
           4: monitorenter
           5: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
           8: ldc           #4                  // String Demo01.main
          10: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
          13: aload_1
          14: monitorexit
          15: goto          23
          18: astore_2
          19: aload_1
          20: monitorexit
          21: aload_2
          22: athrow
          23: return
    
  • 锁的指令
    • monitorenter 加锁
    • monitorexit 释放锁

锁标志位、偏向信息

  • 锁膨胀的原理:通过修改对象头的锁标志位、偏向信息等,从而标识出对象的不同锁状态
  • 锁状态信息存在于对象头中,包括以下几种:
    • 无锁:新建一个对象的默认状态
    • 偏向锁:只需比较 Thread ID,适用于单个线程重入
    • 轻量级锁:CAS自旋,速度快,但存在CPU空转问题、线程长时间获取不到锁问题、CPU高速缓存的频繁同步问题等
    • 重量级锁:需调用系统级互斥锁(mutex/monitor),效率低
    • GC标记:由markSweep使用,标记一个对象为无效状态
  • 看到这里,你应该明白了“为什么synchronized(…)中只能传对象,不能传基础数据类型?”(基础数据类型不是对象,没有对象头,也就没有锁信息)

锁膨胀的流程

Tips:建议对着MarkWord结构图看

  • 执行synchronized同步块,JVM进行优化。
  • —> 单个线程A获取锁,使用CAS修改对象的锁信息(偏向线程id),[无锁]变为[偏向锁]
    • 修改锁对象 MarkWord 中的Thread ID为该线程
    • 锁对象 MarkWord 中的偏向值转为1
  • —> 线程A再次获取锁,因为是偏向锁,所以非常快
  • —> 线程A运行一会儿后,退出了同步代码块
  • —> 线程B过来获取锁,因为锁记录的偏向线程id是线程A,所以进行CAS修改会失败,此时会暂停(安全点)原偏向线程A并检测线程A的状态。因为A之前已退出同步代码块,此时需要线程A释放锁(即修改锁对象为无锁状态)。
  • —> 一会儿后,线程B拿到了偏向锁
  • —> —> 线程C开始过来抢锁,判断锁的偏向线程,显然CAS修改会失败,同时,若线程B仍然存活(未退出同步代码块),原偏向线程B会将 [偏向锁]将升级为[轻量级锁]
    • 在 Thread 栈上建立 LockRecord
    • 拷贝锁对象的 MarkWord 到 Thread 栈上的 LockRecord
    • CAS 替换锁对象的 MarkWord 的 LockRecord 指针 (即指向该 Thread 的 LockRecord)
  • —> —> 线程C持续进行CAS自旋获取执行权(即替换 MarkWord 的 LockRecord 指针),如果成功,获得轻量级锁(CAS自旋存在CPU空转问题)
  • —> —> —> 如果线程C持续进行CAS自旋超过n次(早期版本是 -XX:PreBlockSpin=10,新的是自适应自旋次数),[轻量级锁]将升级为[重量级锁]
    • 锁对象 MarkWord 中的 LockRecord 指针指向系统的重量级锁monitor
    • Thread 进入系统的EntryList队列,等待系统调用,获取到锁
  • 关于膨胀流程,博客dreamtobe有一份示意图如下
    锁膨胀流程图

为什么要进行锁膨胀?较轻的锁就一定好?

  • 首先,由低级别的锁逐步升级到高级别的锁,可以优先使用较轻的锁,一般来说效率更好。
  • 但是,较轻的锁也有自身的问题:
    • 偏向锁,锁标志位是01(和无锁状态一样),通过偏向信息来确定情况,只适合同一线程重入锁的情况(这个时候效率非常高)。当其他线程过来获取锁时,如果竞争激烈,需要经常执行偏向锁撤销与升级为偏向锁的操作,效率较慢。因此,遇到多个线程同时竞争时,需要升级锁,以便于提高效率。
    • 轻量级锁,锁标志位是00,通过CAS自旋实现,效率也不错。但CAS自旋存在浪费CPU性能问题,为避免浪费性能,所以需要规定达到某个自旋次数后(自适应),不再自旋。此时,就需要转为重量级锁。
  • 因此,不能认为较轻的锁(偏向锁、轻量级锁)性能就一定好,应该根据情况达到一个合适的点
  • JVM参数
    • 是否使用偏向锁:默认值 -XX:+UseBiasedLocking

附:OpenJDK8 源码

  • 对象锁状态 hotspot/src/share/vm/oops/markOop.hpp
     enum { locked_value             = 0, // 偏向锁
            unlocked_value           = 1, // 无锁
            monitor_value            = 2, // 重量级锁
            marked_value             = 3, // GC标记
            biased_lock_pattern      = 5  // 偏向锁
     };
    
  • CAS操作 hotspot/src/os_cpu/linux_x86/vm/atomic_linux_x86.inline.hpp (windows同理)
    // int 的 CAS操作
    inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
      int mp = os::is_MP();
      __asm__ volatile (LOCK_IF_MP(%4) "cmpxchgl %1,(%3)"
                        : "=a" (exchange_value)
                        : "r" (exchange_value), "a" (compare_value), "r" (dest), "r" (mp)
                        : "cc", "memory");
      return exchange_value;
    }
    
    // long 的 CAS操作
    inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
      bool mp = os::is_MP();
      __asm__ __volatile__ (LOCK_IF_MP(%4) "cmpxchgq %1,(%3)"
                            : "=a" (exchange_value)
                            : "r" (exchange_value), "a" (compare_value), "r" (dest), "r" (mp)
                            : "cc", "memory");
      return exchange_value;
    }
    
  • 偏向锁 hotspot/src/share/vm/runtime/synchronizer.cpp
    // -----------------------------------------------------------------------------
    //  Fast Monitor Enter/Exit
    // This the fast monitor enter. The interpreter and compiler use
    // some assembly copies of this code. Make sure update those code
    // if the following function is changed. The implementation is
    // extremely sensitive to race condition. Be careful.
    
    void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS) {
     if (UseBiasedLocking) { // JVM参数: -XX:+UseBiasedLocking (是否使用偏向锁)
        if (!SafepointSynchronize::is_at_safepoint()) {
          // 获取偏向锁
          BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
          if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
            return;
          }
        } else {
          assert(!attempt_rebias, "can not rebias toward VM thread");
          BiasedLocking::revoke_at_safepoint(obj);
        }
        assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
     }
    
     slow_enter (obj, lock, THREAD) ;
    }
    
  • 轻量级锁 hotspot/src/share/vm/runtime/synchronizer.cpp
    // -----------------------------------------------------------------------------
    // Interpreter/Compiler Slow Case
    // This routine is used to handle interpreter/compiler slow case
    // We don't need to use fast path here, because it must have been
    // failed in the interpreter/compiler code.
    void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
      markOop mark = obj->mark(); // 获取对象的mark
      assert(!mark->has_bias_pattern(), "should not see bias pattern here");
    
      if (mark->is_neutral()) { // 是否为无锁状态
        // Anticipate successful CAS -- the ST of the displaced mark must
        // be visible <= the ST performed by the CAS.
        lock->set_displaced_header(mark); // 将mark保存到线程的的锁记录中
        // 使用CAS操作将对象的锁记录指针指向 mark
        if (mark == (markOop) Atomic::cmpxchg_ptr(lock, obj()->mark_addr(), mark)) {
          TEVENT (slow_enter: release stacklock) ;
          return ;
        }
        // Fall through to inflate() ...
      } else
      if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
        // 如果处于加锁状态,且mark指向当前线程,继续执行同步代码
        assert(lock != mark->locker(), "must not re-lock the same lock");
        assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
        lock->set_displaced_header(NULL);
        return;
      }
    
    #if 0
      // The following optimization isn't particularly useful.
      if (mark->has_monitor() && mark->monitor()->is_entered(THREAD)) {
        lock->set_displaced_header (NULL) ;
        return ;
      }
    #endif
    
      // The object header will never be displaced to this lock,
      // so it does not matter what the value is, except that it
      // must be non-zero to avoid looking like a re-entrant lock,
      // and must not look locked either.
      lock->set_displaced_header(markOopDesc::unused_mark());
      ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
    }
    
  • 锁膨胀函数
    ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
      // Inflate mutates the heap ...
      // Relaxing assertion for bug 6320749.
      assert (Universe::verify_in_progress() ||
              !SafepointSynchronize::is_at_safepoint(), "invariant") ;
    
      for (;;) { // 自旋
          const markOop mark = object->mark() ;
          assert (!mark->has_bias_pattern(), "invariant") ;
    
          // The mark can be in one of the following states:
          // *  Inflated     - just return
          // *  Stack-locked - coerce it to inflated
          // *  INFLATING    - busy wait for conversion to complete
          // *  Neutral      - aggressively inflate the object.
          // *  BIASED       - Illegal.  We should never see this
    
          // CASE: inflated
          if (mark->has_monitor()) { // 是否已经是重量级锁
              ObjectMonitor * inf = mark->monitor() ; // 如果是,那就获取到monitor,然后返回它
              assert (inf->header()->is_neutral(), "invariant");
              assert (inf->object() == object, "invariant") ;
              assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
              return inf ;
          }
    
          // CASE: inflation in progress - inflating over a stack-lock.
          // Some other thread is converting from stack-locked to inflated.
          // Only that thread can complete inflation -- other threads must wait.
          // The INFLATING value is transient.
          // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
          // We could always eliminate polling by parking the thread on some auxiliary list.
          // 如果处于膨胀中,等待完成(被其他线程执行膨胀)
          if (mark == markOopDesc::INFLATING()) {
             TEVENT (Inflate: spin while INFLATING) ;
             ReadStableMark(object) ;
             continue ; // 继续自旋
          }
    
          // CASE: stack-locked
          // Could be stack-locked either by this thread or by some other thread.
          //
          // Note that we allocate the objectmonitor speculatively, _before_ attempting
          // to install INFLATING into the mark word.  We originally installed INFLATING,
          // allocated the objectmonitor, and then finally STed the address of the
          // objectmonitor into the mark.  This was correct, but artificially lengthened
          // the interval in which INFLATED appeared in the mark, thus increasing
          // the odds of inflation contention.
          //
          // We now use per-thread private objectmonitor free lists.
          // These list are reprovisioned from the global free list outside the
          // critical INFLATING...ST interval.  A thread can transfer
          // multiple objectmonitors en-mass from the global free list to its local free list.
          // This reduces coherency traffic and lock contention on the global free list.
          // Using such local free lists, it doesn't matter if the omAlloc() call appears
          // before or after the CAS(INFLATING) operation.
          // See the comments in omAlloc().
    
          if (mark->has_locker()) { // 是否是轻量级锁
              ObjectMonitor * m = omAlloc (Self) ;
              // Optimistically prepare the objectmonitor - anticipate successful CAS
              // We do this before the CAS in order to minimize the length of time
              // in which INFLATING appears in the mark.
              m->Recycle();
              m->_Responsible  = NULL ;
              m->OwnerIsThread = 0 ;
              m->_recursions   = 0 ;
              m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;   // Consider: maintain by type/class
              
              // 通过CAS操作标识为正在膨胀中
              markOop cmp = (markOop) Atomic::cmpxchg_ptr (markOopDesc::INFLATING(), object->mark_addr(), mark) ;
              if (cmp != mark) {
                 omRelease (Self, m, true) ;
                 // 失败,继续重试自旋(已经被其他线程标识膨胀了)
                 continue ;       // Interference -- just retry
              }
    
              // We've successfully installed INFLATING (0) into the mark-word.
              // This is the only case where 0 will appear in a mark-work.
              // Only the singular thread that successfully swings the mark-word
              // to 0 can perform (or more precisely, complete) inflation.
              //
              // Why do we CAS a 0 into the mark-word instead of just CASing the
              // mark-word from the stack-locked value directly to the new inflated state?
              // Consider what happens when a thread unlocks a stack-locked object.
              // It attempts to use CAS to swing the displaced header value from the
              // on-stack basiclock back into the object header.  Recall also that the
              // header value (hashcode, etc) can reside in (a) the object header, or
              // (b) a displaced header associated with the stack-lock, or (c) a displaced
              // header in an objectMonitor.  The inflate() routine must copy the header
              // value from the basiclock on the owner's stack to the objectMonitor, all
              // the while preserving the hashCode stability invariants.  If the owner
              // decides to release the lock while the value is 0, the unlock will fail
              // and control will eventually pass from slow_exit() to inflate.  The owner
              // will then spin, waiting for the 0 value to disappear.   Put another way,
              // the 0 causes the owner to stall if the owner happens to try to
              // drop the lock (restoring the header from the basiclock to the object)
              // while inflation is in-progress.  This protocol avoids races that might
              // would otherwise permit hashCode values to change or "flicker" for an object.
              // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
              // 0 serves as a "BUSY" inflate-in-progress indicator.
    
    
              // fetch the displaced mark from the owner's stack.
              // The owner can't die or unwind past the lock while our INFLATING
              // object is in the mark.  Furthermore the owner can't complete
              // an unlock on the object, either.
              markOop dmw = mark->displaced_mark_helper() ;
              assert (dmw->is_neutral(), "invariant") ;
    
              // Setup monitor fields to proper values -- prepare the monitor
              m->set_header(dmw) ;
    
              // Optimization: if the mark->locker stack address is associated
              // with this thread we could simply set m->_owner = Self and
              // m->OwnerIsThread = 1. Note that a thread can inflate an object
              // that it has stack-locked -- as might happen in wait() -- directly
              // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
              m->set_owner(mark->locker());
              m->set_object(object);
              // TODO-FIXME: assert BasicLock->dhw != 0.
    
              // Must preserve store ordering. The monitor state must
              // be stable at the time of publishing the monitor address.
              guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
              object->release_set_mark(markOopDesc::encode(m));
    
              // Hopefully the performance counters are allocated on distinct cache lines
              // to avoid false sharing on MP systems ...
              if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
              TEVENT(Inflate: overwrite stacklock) ;
              if (TraceMonitorInflation) {
                if (object->is_instance()) {
                  ResourceMark rm;
                  tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
                    (void *) object, (intptr_t) object->mark(),
                    object->klass()->external_name());
                }
              }
              return m ;
          }
    
          // CASE: neutral
          // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
          // If we know we're inflating for entry it's better to inflate by swinging a
          // pre-locked objectMonitor pointer into the object header.   A successful
          // CAS inflates the object *and* confers ownership to the inflating thread.
          // In the current implementation we use a 2-step mechanism where we CAS()
          // to inflate and then CAS() again to try to swing _owner from NULL to Self.
          // An inflateTry() method that we could call from fast_enter() and slow_enter()
          // would be useful.
    		
    	  // 无锁状态
          assert (mark->is_neutral(), "invariant");
          ObjectMonitor * m = omAlloc (Self) ;
          // prepare m for installation - set monitor to initial state
          m->Recycle();
          m->set_header(mark);
          m->set_owner(NULL);
          m->set_object(object);
          m->OwnerIsThread = 1 ;
          m->_recursions   = 0 ;
          m->_Responsible  = NULL ;
          m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;       // consider: keep metastats by type/class
    
          if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) {
              m->set_object (NULL) ;
              m->set_owner  (NULL) ;
              m->OwnerIsThread = 0 ;
              m->Recycle() ;
              omRelease (Self, m, true) ;
              m = NULL ;
              continue ;
              // interference - the markword changed - just retry.
              // The state-transitions are one-way, so there's no chance of
              // live-lock -- "Inflated" is an absorbing state.
          }
    
          // Hopefully the performance counters are allocated on distinct
          // cache lines to avoid false sharing on MP systems ...
          if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
          TEVENT(Inflate: overwrite neutral) ;
          if (TraceMonitorInflation) {
            if (object->is_instance()) {
              ResourceMark rm;
              tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
                (void *) object, (intptr_t) object->mark(),
                object->klass()->external_name());
            }
          }
          return m ;
      }
    }
    
  • 关于自适应自旋
    • 请查阅hotspot/src/share/vm/runtime/objectMonitor.cpp 中的 _SpinDuration
synchronized锁原理分析(二、锁的膨胀过程) 继续承接上一篇博客一、从Java对象头看synchronized锁的状态 先通过几个案例,从结果直观的展示锁是如何膨胀的 最后将锁的膨胀过程通过一张流程图展示出来 案例1(无锁,不可偏向状态) import org.openjdk.jol.info.ClassLayout; public class Demo1 { public static void main(String[] args) throws InterruptedException { A a = new A(); /// 理论上说这里应该是无锁状态 System.ou 立即下载

相关推荐

soul聊天协议逆向

通过观察上面的CFG发现,该混淆符合以下逻辑,整体思路就是先查找主分发器,一般被引用次数最多的那个块就是主分发器,引用主分发器的块大概率是真实块,再使用unicorn模拟执行,遍历每一个分支,记录每一个块,如果某一个块是上次记录的块则也为真实块,无后继的块则为retn块,剩下的就是无用块。通过Frida Hook libc.so后发现,聊天协议走的是JAVA层的socket,然后Hook JAVA层相关函数得到如下调用堆栈。里面调用的函数不是很多,所以。不行不行,革命要彻底!

2503_90751789的博客 3236

线程学习(12)-锁膨胀

锁膨胀其实就是锁升级的过程,从偏离锁升级为轻量级锁,以及从轻量级锁升级成重量级锁的过程。这里以从轻量级锁升级成重量级锁的过程直接上代码吧。 轻量级锁升级成重量级锁的过程过程。 package com.bo.threadstudy.four; import lombok.extern.slf4j.Slf4j; import org.openjdk.jol.info.ClassLayout; import java.util.concurrent.locks.LockSupport; /** *

key_wu的博客 806

全国30米分辨率逐年植被覆盖度(FVC)数据集

植被覆盖度是描述地表植被的重要参数与生态环境的基本指标,在大气圈、土壤圈、水圈和生物圈中都占据着重要的地位,区域及全球范围的植被覆盖度估算对植被及相关领域的研究具有重要意义。往往参照《土壤侵蚀分类分级标准》,将植被覆盖度划分为4个等级:<30%为低覆盖度、30%—45%为中低覆盖度、45%—60%为中覆盖度、>60%为高覆盖度。本数据集包括1986至2022年间,全国植被覆盖度数据,FVC范围值为0-1,数据为浮点型,GeoTIFF格式。NDVImax 和NDVImin分别为区域内最大和最小的NDVI值。

地球资源数据云的官方博客 1977

synchronized锁膨胀过程

锁的状态总共有四种:无锁状态、偏向锁、轻量级锁和重量级锁。随着锁的竞争,锁可以从偏向锁升级到轻量级锁,再升级的重量级锁(但是锁的升级是单向的,也就是说只能从低到高升级,不会出现锁的降级)。JDK1.6中默认是开启偏向锁和轻量级锁的,我们也可以通过-XX:-UseBiasedLocking来禁用偏向锁。

道阻且长,行则将至! 717

Python3.8下MvCameraControl.dll加载失败?3种方法彻底解决FileNotFoundError

本文针对Python3.8及以上版本在Windows平台调用MvCameraControl.dll时出现的FileNotFoundError问题,深入剖析了其根源在于Python 3.8引入的DLL加载安全机制变更。文章提供了三种核心解决方案:显式指定DLL完整路径、使用os.add_dll_directory()添加可信目录,以及利用ctypes的winmode参数进行精细控制,并给出了工程化封装建议,帮助开发者彻底解决此兼容性问题。

weixin_29093017的博客 201

手把手教你搭建硬件在回路(HIL)仿真系统:从Simulink建模到实时测试

本文详细介绍了如何从零开始搭建硬件在回路(HIL)仿真系统,涵盖Simulink建模、实时仿真、代码生成及调试等关键步骤。通过具体配置示例和优化技巧,帮助开发者解决复杂控制系统验证中的技术难题,提升半实物仿真的效率和可靠性。

weixin_30952535的博客 450

synchronized锁的膨胀过程详细介绍

synchronized锁的膨胀过程详细介绍

java架构师分享,有问题可以联系我 781

5大步骤!轻松玩转Dorisoy.Rigger:基于.NET Core 6、MAUI和Blazor的多数据库开发框架生成器

各位小伙伴,你们是否厌倦了每次项目启动都要从头配置数据库连接、编写重复的代码?今天,我们就来聊聊如何使用Dorisoy.Rigger这个强大的开发框架生成器,让你的.NET Core 6、MAUI和Blazor项目开发变得简单又高效!准备好迎接开发的新乐趣了吗?让我们一起开始吧!接下来,我们需要定义数据模型。假设我们有一个简单的用户表,包含ID、用户名和密码字段。在项目中创建一个新的类文件,命名为User.csset;set;set;这段代码定义了一个用户模型,其中包含了用户的ID、用户名和密码。

java专栏 1749

学习笔记分享-jvm-synchronized-(轻量级锁、锁膨胀、重量级锁-自旋、偏向锁、其他优化)

学习笔记分享-jvm-synchronized-(轻量级锁、锁膨胀、重量级锁-自旋、偏向锁、其他优化)

2301_81243975的博客 615

【电源】电机一启动或负载电流瞬间增大,电源输出电压就下降,欠压,随后又恢复正常

系列文章目录 1.元件基础 2.电路设计 3.PCB设计 4.元件焊接 5.板子调试 6.程序设计 7.算法学习 8.编写exe 9.检测标准 10.项目举例 11.职业规划 亲身经历,特记录下来,分享经验 一、问题描述 用的时创联B-150-24开关电源,这个电源可以同时输出24V 5.8A和5V 2A。5V输出端接的是一个固定1.3A的负载。24V端接的是0.7A的固定负载和一个直流电机。开机后,开启直流电机瞬间,此时直流电机上流过的电流最大是2.5A,24V端口处电流测得0.9A,电源盒

云轩阁 6800

FPGA打砖块游戏设计(有上板照片)VHDL

这是一款经典打砖块游戏,我们的努力让它更精致更好玩,我们将它取名为打砖块游戏(Flyball),以下是该系统的一些基本功能: 画面简约而经典,色彩绚丽而活泼,动画流畅 玩家顺序挑战3个不同难度的级别,趣味十足 计分功能,卡通字母数字 4条生命值,由生命条显示 游戏结束画面,缓缓浮起与缤纷刷色的特效 四键操作,可复位,可暂停,高灵敏 96块砖拼出可爱的“囧”字,方便更改 碰挡板非镜面反射 初始启动,随机的发射速度。

QQ_778132974的博客 1665

【谈一谈】并发_Synchronized

又到周末了,最近的话(有点子小日子不好过,哈哈哈!~)但是,我还是哈哈哈本次写关于并发_的优化以及底层实现原理说说心里话~

aristo 1047

直流无刷电机FOC控制算法 理论到实践 —— 实践

关于直流无刷电机FOC控制算法的理论章节,本人已经在前两章进行过详细说明,大家可以自行进行阅读,请务必了解过理论之后再来学习如何具体进行实现。直流无刷电机FOC控制算法 理论到实践 —— 理论(一)直流无刷电机FOC控制算法 理论到实践 —— 理论(二)本章节主要讨论采用MCU微控制器如何具体实现直流无刷电机的FOC控制。

爱出名的狗腿子 1万+

java锁Synchronized锁膨胀

在java中,synchronized锁的膨胀是指锁从轻量级状态逐步升级为更重的状态,以适应并发情况的变化。这种锁升级机制是为了在不同的线程竞争强度下提供合适的锁实现,最大化性能。锁的膨胀过程从“无锁”到“偏向锁”,再到“轻量级锁”,最终可能升级为“重量级锁”。这个机制通过减少不必要的线程阻塞和上下文切换,提高了性能。

weixin_41863236的博客 2018

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Java并发编程:Synchronized 的优化机制

今天我们首先介绍了一下用户态和内核态的内容。通常我们的应用代码都是用户态的,会和系统的内核交互,这样做的好处是保证系统权限的安全。之后,我们介绍了 JVM 底层对锁的几种优化方式,其中自旋可能是大家最为熟悉的操作。但要注意的是,在很多 CAS 的场景中需要我们手动地通过编码的方式完成自旋操作,而本文提到的自旋是 JVM 底层帮我们做的事情,这是大家要注意区分的地方。从 JDK1.6 开始,JVM 帮我们实现的自旋,变得很智能,可以自动选择自旋的次数。

m0_46995061的博客 602

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

synchronized优化手段:锁膨胀、锁消除

可能有人会问我为什么愿意去花时间帮助大家实现求职梦想,因为我一直坚信时间是可以复制的。我牺牲了自己的大概十个小时写了这片文章,换来的是成千上万的求职者节约几天甚至几周时间浪费在无用的资源上。上面的这些(算法与数据结构)+(Java多线程学习手册)+(计算机网络顶级教程)等学习资源《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!与数据结构)+(Java多线程学习手册)+(计算机网络顶级教程)等学习资源。

2401_83817122的博客 936

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Java锁优化全解析:偏向锁、轻量级锁、重量级锁的演进与废除

坑点原因解决方案hashCode 导致偏向锁失效Mark Word 空间被 hashCode 占用避免在加锁前调用 hashCode延迟启动导致测试偏差JVM 4 秒延迟测试时等待或使用参数归零批量撤销性能抖动类级别撤销触发关闭偏向锁或使用线程本地对象轻量级锁自旋空转竞争线程多适当增加锁状态标志位优点缺点适用场景无锁01无开销无法同步未进入同步块偏向锁01单线程零开销撤销昂贵、已废弃单线程独占(历史)轻量级锁00自旋等待,无内核切换。

开发小能手-Haha king的博客 454

JVM对synchronized的优化

在JDK1.6之后JVM对synchronized底层进行了优化,提高了并发访问性能得到提升: 第一点:锁的膨胀升级过程是最大的优化 第二点:锁的粗化 第三点:锁的消除 锁的膨胀升级过程: 无锁–》偏向锁–》轻量级锁–》重量级锁(互斥锁) 锁优化后一共有四种状态,无锁、偏向锁、轻量级锁、重量级锁。一开始从new出来的对象没有线程使用和加锁是无锁状态,然后随着锁的竞争,锁可以从偏向锁升级到轻量级锁,再升级的重 量级锁,但是锁的升级是单向的,也就是说只能从低到高升级,不会出现锁的降级 偏向锁: 偏向锁是Jav

shaowei6969的博客 538

【P0】Java 锁篇-精炼

本文摘要: Java中的读写锁技术通过ReentrantReadWriteLock实现了读/写操作的分离控制。其主要特性包括:读锁(共享锁)允许多线程并发读取,写锁(排他锁)保证写操作的原子性,特别适合读多写少的场景。与普通互斥锁相比,它能显著提升系统吞吐量。该锁支持公平/非公平模式、可重入特性、锁降级(写锁转为读锁)和中断获取,但不支持锁升级。内存设计确保数据一致性,且仅写锁提供Condition支持。典型应用包括缓存查询等并发读取需求高的场景。

weixin_50999696的博客 1059

深入理解 synchronized 底层实现:从 HotSpot C++ 源码看对象锁与 Monitor 机制

是 Java 中最基础的线程同步关键字,几乎每个 Java 开发者都用过。为什么任意一个 Java 对象都能作为锁?加锁解锁,JVM 底层到底做了什么?偏向锁、轻量级锁、重量级锁的"升级"过程,在 C++ 代码中是如何体现的?wait()notify()的等待队列,底层数据结构长什么样?本文将深入HotSpot VM 的 C++ 源码,从oopDescmarkWord三个核心结构出发,彻底揭开的底层面纱。

zhoujie的博客 494

synchronized 底层实现原理、重量级锁、轻量锁、锁膨胀、锁自旋、偏向锁详解

在JVM中,对象拥有一个“对象头”,格式如下,其中包括了标记字(Mark Word)和类型指针(Class Pointer),Klass Word指向的是该对象所属的类,而Mark Word中记录了对象的一些状态信息,包括对象的哈希码、锁状态和GC相关信息等。在上文中可知,轻量级锁在没有竞争时(还是这个线程来获取锁),将会发生锁重入,要执行CAS操作,并在栈桢创建新的锁记录。当有线程(称为Thread-2)运行如下程序时,即在给对象obj上锁,此时,该对象头的Mark Word,若还有别的线程,则同上。

qurious7的博客 1991

synchronized优化原理

锁,轻量级锁,偏向锁,锁消除

qq_43456605的博客 234

Java JDK1.6对Synchronized锁优化引入锁膨胀、锁消除、锁粗化、自旋锁、偏向锁、轻量级锁、重量级锁详解

monitor原理及执行步骤 因为每个对象头中都存在monitor锁对象,所以java中可以用对象加锁。 对象结构大概分为对象头、实例变量和填充字节。 对象头 Mark Word(标记字段):存储对象的hashCode、锁信息或分代年龄或GC标志等信息 Klass Pointer(类型指针):对象指向它的类元数据的指针,虚拟机通过这个指针来确定这个对象是哪个类的实例 数组长度:只有数组对象保存了这部分数据。该数据在32位和64位JVM中长度都是32bit。 实例变量:实例数据部分是对象真正存储的有效

weixin_41590779的博客 441

【并发编程】synchronized关键字最全详解,看这一篇就够了

目录 引入 一、synchronized的特性 1.1 原子性 1.2 可见性 1.3 有序性 1.4 可重入性 二、synchronized的用法 根据修饰对象分类 1.同步方法 2.同步代码块 根据获取的锁分类 1.获取对象锁 2.获取类锁 三、synchronized锁的实现 3.1 为什么synchronized通过锁机制可以保证原子性,可见性和有序性,其...

小七的博客 2240

synchronized 锁升级路径深度解析

摘要:本文深度解析synchronized锁升级路径,从无锁状态到重量级锁的完整演进过程。详细剖析了偏向锁、轻量级锁和重量级锁的实现机制,包括Mark Word内存布局、锁状态转换条件及优化策略。重点讲解了对象头结构(64位JVM下)、锁撤销流程、自旋优化以及生产环境调优方法。通过状态机图示和代码示例,展示了不同锁状态下Mark Word的位域变化,为开发者提供从理论到实践的完整锁升级知识体系,帮助优化高并发场景下的锁性能。

qq_43414012的博客 1345
上一篇: 网络通信框架——KyroNet示例
下一篇: MySQL中的索引 B+Tree
蒋含竹
博客等级 码龄10年 437粉丝 196原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值