Java中互斥锁信号量与多线程等待机制的示例
这篇文章主要介绍了Java编程中的互斥锁,信号量和多线程等待机制实例详解,简单介绍了互斥锁和信号量的区别,需要的朋友可以了解下。
互斥锁和信号量都是操作系统中为并发编程设计基本概念,互斥锁和信号量的概念上的不同在于,对于同一个资源,互斥锁只有0和1 的概念,而信号量不止于此。也就是说,信号量可以使资源同时被多个线程访问,而互斥锁同时只能被一个线程访问
互斥锁在java中的实现就是 ReetranLock , 在访问一个同步资源时,它的对象需要通过方法 tryLock() 获得这个锁,如果失败,返回 false,成功返回true。根据返回的信息来判断是否要访问这个被同步的资源。看下面的例子
public class ReentranLockExample { private static int count = 0; private static ReentrantLock reentrantLock = new ReentrantLock(); static class MyThread extends Thread{ @Override public void run() { super.run(); try { while (true){ boolean result = reentrantLock.tryLock(); if (result){ System.out.println(Thread.currentThread().getName() + "get the lock success and run the syn code " + count ++); reentrantLock.unlock(); }else{ System.out.println(Thread.currentThread().getName() + "get the lock failed and run the syn code " + count); } System.out.println(Thread.currentThread().getName() + "run the asyntronized code " + count); Thread.sleep(500); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args){ MyThread thread1 = new MyThread(); MyThread thread2 = new MyThread(); thread1.start(); thread2.start(); } }
信号量相当于一个计数器,如果线程想要访问某个资源,则先要获得这个资源的信号量,并且信号量内部的计数器减1 ,信号量内部的计数器大于0则意味着有可以使用的资源,当线程使用完某个资源时,必须释放这个资源的信号量。信号量的一个作用就是可以实现指定个线程去同事访问某个资源。只需要在初始化 。
信号量在 Java中的实现是 Semaphore ,其在初始化时传入一个整型数, 用来指定同步资源最大的并发访问量
public class SemaphoreExample { private static Semaphore semaphore = new Semaphore(2); private String lock = "lock"; private static int count = 0; static class MyThread extends Thread { @Override public void run() { super.run(); try { while (true) { semaphore.acquire(); Thread.sleep(500); System.out.println(Thread.currentThread().getName() + "get the lock success and run the syn code " + count++); semaphore.release(); Thread.sleep(500); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args){ MyThread thread1 = new MyThread(); MyThread thread2 = new MyThread(); MyThread thread3 = new MyThread(); thread1.start(); thread2.start(); thread3.start(); } }
CountDownLatch 实现一个等待机制,在诸如 等待与会者到达后,开始会议的使用中。ConutDownLatch 在初始化中一个计数器,用来指定需要等待的个数。在并发编程中,所解决的需求就是,等待所有的线程到达某个点后。才开始进行下一步,有点类似于开会,只有当所有的与会人员都到齐后,会议才能开始
public class CountDownLatchExample { private static CountDownLatch mCountDownLatch = new CountDownLatch(3); static class MyThread extends Thread { int awaitTime; public MyThread(int i) { this.awaitTime = i; } @Override public void run() { super.run(); try { while (true) { Thread.sleep(awaitTime); System.out.println(Thread.currentThread().getName() + "arrived " ); mCountDownLatch.countDown(); mCountDownLatch.await(); //可以指定等待时间 System.out.println(Thread.currentThread().getName() + "start meeting " ); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args){ MyThread thread1 = new MyThread(500); MyThread thread2 = new MyThread(1000); MyThread thread3 = new MyThread(2000); thread1.start(); thread2.start(); thread3.start(); } }
总结
以上是Java中互斥锁信号量与多线程等待机制的示例的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

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

热门话题

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
