当前位置: 首页 > news >正文

吉林华商建设集团网站游戏前端转网站开发

吉林华商建设集团网站,游戏前端转网站开发,wordpress倒闭汉化组,seo是什么工作从源码学习LockSupport 2024年6月30日 大家好啊#xff0c;好久没写博客了#xff0c;今天打算写一下#xff0c;讲一下JUC里面LockSupport这个类。 这个是一个工具类#xff0c;实际上也是为了线程通信开发的。它的源码比较短#xff0c;也只引用了Unsafe一个类。所以…从源码学习LockSupport 2024年6月30日 大家好啊好久没写博客了今天打算写一下讲一下JUC里面LockSupport这个类。 这个是一个工具类实际上也是为了线程通信开发的。它的源码比较短也只引用了Unsafe一个类。所以我们可以先看下他的源码 类注释 他的类注释如下 Basic thread blocking primitives for creating locks and other synchronization classes. This class associates, with each thread that uses it, a permit (in the sense of the Semaphore class). A call to park will return immediately if the permit is available, consuming it in the process; otherwise it may block. A call to unpark makes the permit available, if it was not already available. (Unlike with Semaphores though, permits do not accumulate. There is at most one.) Reliable usage requires the use of volatile (or atomic) variables to control when to park or unpark. Orderings of calls to these methods are maintained with respect to volatile variable accesses, but not necessarily non-volatile variable accesses. Methods park and unpark provide efficient means of blocking and unblocking threads that do not encounter the problems that cause the deprecated methods Thread. suspend and Thread. resume to be unusable for such purposes: Races between one thread invoking park and another thread trying to unpark it will preserve liveness, due to the permit. Additionally, park will return if the caller’s thread was interrupted, and timeout versions are supported. The park method may also return at any other time, for “no reason”, so in general must be invoked within a loop that rechecks conditions upon return. In this sense park serves as an optimization of a “busy wait” that does not waste as much time spinning, but must be paired with an unpark to be effective. The three forms of park each also support a blocker object parameter. This object is recorded while the thread is blocked to permit monitoring and diagnostic tools to identify the reasons that threads are blocked. (Such tools may access blockers using method getBlocker(Thread).) The use of these forms rather than the original forms without this parameter is strongly encouraged. The normal argument to supply as a blocker within a lock implementation is this. These methods are designed to be used as tools for creating higher-level synchronization utilities, and are not in themselves useful for most concurrency control applications. The park method is designed for use only in constructions of the form: while (!canProceed()) { // ensure request to unpark is visible to other threads ... LockSupport. park(this); }where no actions by the thread publishing a request to unpark, prior to the call to park, entail locking or blocking. Because only one permit is associated with each thread, any intermediary uses of park, including implicitly via class loading, could lead to an unresponsive thread (a “lost unpark”). Sample Usage. Here is a sketch of a first-in-first-out non-reentrant lock class: class FIFOMutex {private final AtomicBoolean locked new AtomicBoolean(false);private final QueueThread waiters new ConcurrentLinkedQueue();public void lock() {boolean wasInterrupted false;// publish current thread for unparkerswaiters.add(Thread.currentThread());// Block while not first in queue or cannot acquire lockwhile (waiters.peek() ! Thread.currentThread() || !locked.compareAndSet(false, true)) {LockSupport.park(this);// ignore interrupts while waiting if (Thread.interrupted()) wasInterrupted true;}waiters.remove();// ensure correct interrupt status on returnif (wasInterrupted)Thread.currentThread().interrupt();}public void unlock() {locked.set(false);LockSupport.unpark(waiters.peek());}static { // Reduce the risk of lost unpark due to classloadingClass? ensureLoaded LockSupport.class;} }第一段文本大意为 用于创建锁和其他同步类的基本线程阻塞原语。这个类与使用它的每个线程关联一个许可证(在信号量类的意义上)。如果许可证可用将立即返回停车呼叫并在此过程中消耗许可证;否则可能会堵塞。如果许可证尚未可用则调用取消停车使其可用。(但与信号量不同的是许可证不会累积。最多只有一个。)可靠的使用需要使用易失性(或原子)变量来控制何时停放或取消停放。调用这些的顺序 其实就是对想要枷锁的信息加上一个许可提前给他许可再消费许可还是代码走到消费许可后再拿到许可都可以。 第二段文本大意为 如果在调用park之前线程没有发布请求取消park的操作则会导致锁定或阻塞。因为每个线程只关联一个许可证任何中间使用park(包括通过类加载隐式地使用)都可能导致线程无响应(“丢失的unpark”)。 样品使用。这是一个先进先出的不可重入锁类的草图: 就是根据第二块代码展示LockSupport具体怎么使用。 代码细节 unpark(Thread thread)给对应thread发放许可。如果线程被LocjSupport的park()方法阻塞时使用该方法会被立刻释放运行如果没有阻塞则使用该方法发放许可证明时在下一次调用park()方法则会直接运行不被阻塞。 park(Object blocker)会向当前线程请求许可没有许可就会阻塞除非发生以下三种情况之一: 其他线程以当前线程为目标调用unpark其他线程中断当前线程调用返回 但是park方法不能看出来是哪一个原因导致线程可以重新运行。需要调用的人自己写代码看例如线程返回时的中断状态。 parkNanos(Object blocker, long nanos)和park方法一样但是有时间限制到了时间限制也会停止阻塞。 getBlocker(Thread t)提供阻塞线程t的正在被阻塞的park方法是谁调用的但是线程不安全拿到这个对象操作的时候可能也已经被unpark了也说不定。 park()直接禁用当前线程。 private LockSupport() {} // Cannot be instantiated.LockSupport不能被初始化只能使用LockSupport进行开发 setCurrentBlocker(Object blocker)该方法可以做到在blocker使用park()方法之前会调用该方法可以做到一些分析的作用。 总结 这个方法比较简单但是比较方便进行线程间通信。
http://www.w-s-a.com/news/348903/

相关文章:

  • 宿州外贸网站建设公司个人注册网站一般做什么
  • 小公司做网站用哪种服务器什么是网站代理
  • 青岛李村网站设计公司cms建站平台
  • 做saas网站可行吗许昌抖音推广公司
  • 网站建设找谁做seo基础知识培训
  • 微网站怎么做的好建设网站不会写代码
  • 广州外贸网站制作wordpress信息搜索插件
  • 福建高端网站建设个人公众号怎么制作教程
  • 企业网站有哪些举几个例子wordpress ie兼容插件
  • 高端的深圳网站页面设计福清市建设局官方网站
  • 安装网站到服务器合肥建设干部学校网站
  • 影视网站如何做销售案例网站
  • 建设网站对比方案龙岗网站开发公司
  • 网站开发标准网站建设公司兴田德润可信赖
  • 如何建设一个公众号电影网站自动seo优化
  • 个人网站能备案吗酱香拿铁采取了哪些网络营销方式
  • 网站建设及推广好做吗自己做的网站加入购物车价格
  • 涡阳在北京做网站的名人注册一个免费的网站
  • 三门峡建设环境局网站公司注册网上核名通道
  • 叶县建设局网站要看网海外域名是多少
  • 网站运行环境配置Wordpress支付时效
  • logo设计网站知乎港北网站建设
  • 北京市保障性住房建设投资中心官方网站有限责任公司的特点
  • 做网站卖互联网营销怎么做
  • 晋州市建设局网站建站网站系统
  • 专业网站优化方案广东微信网站制作报价表
  • 北京网站建设公司分形科技简述营销网站建设策略
  • 汉中网站建设有限公司vue网站开发
  • 网站备案背景幕布阳江东莞网站建设
  • 北京网站建设要多少钱html网站标签