JAVA并发编程有界缓存的实现
1、有界缓存的基类
package cn.xf.cp.ch14; /** * *功能:有界缓存实现基类 *时间:下午2:20:00 *文件:BaseBoundedBuffer.java *@author Administrator * * @param <V> */ public class BaseBoundedBuffer<V> { private final V[] buf; private int tail; private int head; private int count; public BaseBoundedBuffer(int capacity) { //初始化数组 this.buf = (V[]) new Object[capacity]; } //放入一个数据,final方法无法被重写 protected synchronized final void doPut(V v) { buf[tail] = v; if(++tail == buf.length) { tail = 0; } //插入一个方法,总量++ ++count; } /** * 取出一个数据 * @return */ protected synchronized final V doTake() { V v = buf[head]; buf[head] = null; if(++head == buf.length) { head = 0; } --count; return v; } //通过对count的判断,来确定数组是否是满的 public synchronized final boolean isFull() { return count == buf.length; } public synchronized final boolean isEmpty() { return count == 0; } }
2、判定前提条件再执行操作
package cn.xf.cp.ch14; /** * *功能:对插入和获取元素操作进行先行检查,然后执行操作,校验不通过不予操作 *时间:下午2:33:41 *文件:GrumpyBoundedBuffer.java *@author Administrator * * @param <V> */ public class GrumpyBoundedBuffer<V> extends BaseBoundedBuffer<V> { public GrumpyBoundedBuffer(int size) { super(size); } public synchronized void put(V v) throws Exception { //如果是满的队列,就无法插入新的元素 if(this.isFull()) { throw new Exception("队列超出"); } this.doPut(v); } //同理,队列为空的就无法取出新的元素 public synchronized V take() throws Exception { if(this.isEmpty()) { throw new Exception("队列中无元素"); } return this.doTake(); } }
3、通过轮询与休眠来实现简单的阻塞
package cn.xf.cp.ch14; /** * *功能:通过轮询与休眠来实现简单的阻塞 *时间:下午2:55:54 *文件:SleepyBoundedBuffer.java *@author Administrator * * @param <V> */ public class SleepyBoundedBuffer<V> extends BaseBoundedBuffer<V> { //2s private static final long SLEEP_GRANULARITY = 2000; public SleepyBoundedBuffer(int capacity) { super(capacity); } //放入队列的时候 public void put(V v) throws InterruptedException { while(true) { //这里不对循环上锁,不然这个锁就无法释放了,不对休眠上锁,休眠上锁,在休眠的时候别人也无法操作,永远都不可能有元素出去 synchronized (this) { //如果队列不是满的,那么就放入元素 if(!this.isFull()) { this.doPut(v); return; } } //否则休眠,退出cpu占用 Thread.sleep(SLEEP_GRANULARITY); } } public V take() throws InterruptedException { while(true) { //这里不对循环上锁,不然这个锁就无法释放了,不对休眠上锁,休眠上锁,在休眠的时候别人也无法操作,永远都不可能有新的元素进来 synchronized(this) { //如果数组部位空,那么就可以取出数据 if(!this.isEmpty()) { return this.doTake(); } //如果队列为空,休眠几秒再试 } Thread.sleep(SLEEP_GRANULARITY); } } }
4、条件队列
package cn.xf.cp.ch14; /** * *功能:使用条件队列 *时间:下午3:32:04 *文件:BoundedBuffer.java *@author Administrator * * @param <V> */ public class BoundedBuffer<V> extends BaseBoundedBuffer<V> { public BoundedBuffer(int capacity) { super(capacity); } /** * 放入数据元素 * @param v * @throws InterruptedException */ public synchronized void put(V v) throws InterruptedException { while(this.isFull()) { //这里挂起程序,会释放锁 this.wait(); } //如果队列不为满的,那么程序被唤醒之后从新获取锁 this.doPut(v); //执行结束,唤醒其他队列 this.notifyAll(); } public synchronized V take() throws InterruptedException { while(this.isEmpty()) { this.wait(); } V v = this.doTake(); this.notifyAll(); return v; } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
更多JAVA并发编程有界缓存的实现详解相关文章请关注PHP中文网!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代码在不同平台上运行时会有性能差异。1)JVM的实现和优化策略不同,如OracleJDK和OpenJDK。2)操作系统的特性,如内存管理和线程调度,也会影响性能。3)可以通过选择合适的JVM、调整JVM参数和代码优化来提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑战WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3汉化版
中文版,非常好用

记事本++7.3.1
好用且免费的代码编辑器