当前位置: 移动技术网 > IT编程>开发语言>Java > 详解java中的互斥锁信号量和多线程等待机制

详解java中的互斥锁信号量和多线程等待机制

2019年07月19日  | 移动技术网IT编程  | 我要评论

互斥锁和信号量都是操作系统中为并发编程设计基本概念,互斥锁和信号量的概念上的不同在于,对于同一个资源,互斥锁只有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编程中的互斥锁,信号量和多线程等待机制实例详解的全部内容,希望对大家有所帮助。

有兴趣的朋友可以了解:java多线程卖票实例java多线程并发编程(互斥锁reentrant lock)等。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网