semaphore

CountDownLatch、CyclicBarrier、Semaphore、Exchanger

自作多情 提交于 2020-03-25 09:19:38
CountDownLatch: 允许N个线程等待其他线程完成执行。无法进行重复使用,只能用一次。 比如有2个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。 public class Test { public static void main(String[] args) { final CountDownLatch latch = new CountDownLatch(2); new Thread(){ public void run() { try { System.out.println("子线程"+Thread.currentThread().getName()+"正在执行"); Thread.sleep(3000); System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕"); latch.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); new Thread(){ public void run() { try { System.out.println("子线程"+Thread.currentThread()

信号量 - semaphore

房东的猫 提交于 2020-03-22 17:12:27
一. POSIX - 信号量 #include <semaphore.h> sem_t sem; ///< 信号量 信号量,分为有名信号量 和无名信号量。 有名信号量由sem_open/sem_close/sem_unlink创建/关闭/销毁,用于进程间通信。 无名信号量由sem_init/sem_destroy创建/销毁,用于线程间通信。 1. 信号量初始化 /*********************************************************** * @param[sem] 非命名信号量,只能被sem_destroy()销毁, * @param[pshared] 非0表示进程间通信信号量,但是Linux系统暂未实现这一功能(实现方式为共享内存),0表示线程间通信信号量。 * @param[value] 信号量初始化值 * @return 成功返回0,失败返回-1及设置错误码errno *//********************************************************/ int sem_init(sem_t * sem, int pshared, unsigned vlaue); /*********************************************************** *

Semaphore vs Mutex in Producer/Consumer

☆樱花仙子☆ 提交于 2020-03-22 09:22:28
问题 In the Producer-Consumer problem, why are we often suggested use to semaphores instead of using a lock/mutex? I don't see a valid reason to use a semaphore because we only have 2 threads coordinating. In this case a lock seems much easier to code and reason about because a thread will lock the buffer then free it so the other thread can do the same. There are only 2 threads so I don't see the use of signaling. Can anyone say why it is suggested to use semaphores usually for producer-consumer?

CountDownLatch,CyclicBarrier,Semaphore

送分小仙女□ 提交于 2020-03-21 15:25:06
CountDownLatch是倒数,doneSignal = new CountDownLatch(LATCH_SIZE);赋初值后,在主线程中等待doneSignal.await();其它线程中,每完成一个就减一doneSignal.countDown();减到0时主线程继续。 CyclicBarrier是正数,cb = new CyclicBarrier(SIZE);主线程中开启各子线程,子线程调用cb.await()进行等待;cb计数count会加一,等于SIZE时会继续所有等待线程。 Semaphore是信号灯,Semaphore sem = new Semaphore(SEM_MAX);主线程中开启各子线程,子线程调用sem.acquire(count);每调用一次,sem计数会减相应数值,减为0时其它线程再调用acquire时会阻塞,线程结束后记得要sem.release(count); CountDownLatch和Semaphore用的是共享锁,而CyclicBarrier是独占锁。 CountDownLatch只能赋值一次,而CyclicBarrier可赋值多次。 概要 前面对" 独占锁 "和" 共享锁 "有了个大致的了解;本章,我们对CountDownLatch进行学习。 和ReadWriteLock.ReadLock一样

并发工具类和线程池

…衆ロ難τιáo~ 提交于 2020-03-19 17:53:22
工具类 CountDownLatch 利用它可以实现类似计数器的功能。比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。 package com.yjc.juc; import java.util.concurrent.CountDownLatch; public class CountDownLatchDemo { public static void main(String[] args) throws InterruptedException { System.out.println("主线程启动---->等待子线程执行完毕"); //代表等待两个线程执行完主线程才继续执行 CountDownLatch countDownLatch=new CountDownLatch(2); new Thread(() -> { System.out.println("第一个子线程" + Thread.currentThread().getName() + "正在执行"); countDownLatch.countDown(); System.out.println("第一个子线程" + Thread.currentThread().getName() + "执行完毕"); }).start(); new Thread(

CountDownLatch, CyclicBarrier and Semaphore

混江龙づ霸主 提交于 2020-03-18 15:35:05
Reference: [1] http://shazsterblog.blogspot.co.uk/2011/12/comparison-of-countdownlatch.html CountDownLatch vs CyclicBarrier CountDownLatch can not be reused after meeting the final count. CountDownLatch can not be used to wait for Parallel Threads to finish. CyclicBarrier can be reset thus reused CyclicBarrier can be used to wait for Parallel Threads to finish. CountDownLatch CountDownLatch can be used to monitor the completion of the Children Threads if the size of the created children is known forehand. CountDownLatch enables a Thread or Threads to wait for completion of Children Threads.

并发工具——Semaphore

心不动则不痛 提交于 2020-03-08 18:24:18
Semaphore的使用和操作系统中的信号量的使用差不多,可以类比去理解。 Semaphore用于限制可以访问某些资源(物理或逻辑的)的线程数目,他维护了一个许可证集合,有多少资源需要限制就维护多少许可证集合,假如这里有N个资源,那就对应于N个许可证,同一时刻也只能有N个线程访问。一个线程获取许可证就调用acquire方法,用完了释放资源就调用release方法。 停车场案例代码实现: public class CarDemo { public static void main(String[] args) { //创建Semaphore Semaphore sp=new Semaphore(5); Thread[] car=new Thread[10]; for (int i = 0; i < 10; i++) { car[i]=new Thread(()->{ //请求许可 try { sp.acquire(); System.out.println(Thread.currentThread().getName()+"可以进停车场"); } catch (InterruptedException e) { e.printStackTrace(); } //使用资源 try { int val= new Random().nextInt(10); TimeUnit.SECONDS

C# SemaphoreSlim array elements read/write synchronization [closed]

醉酒当歌 提交于 2020-03-04 20:05:42
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 days ago . My blocking ring queue don't use lock or Mutex , only two SemaphoreSlim (block than 0 and than max element, so write and read part of array never intersects) and two int indexes modified by Interlocked.Decrement (not determine write/read index, but make it unique and correct move).

C# SemaphoreSlim array elements read/write synchronization [closed]

末鹿安然 提交于 2020-03-04 20:05:13
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 days ago . My blocking ring queue don't use lock or Mutex , only two SemaphoreSlim (block than 0 and than max element, so write and read part of array never intersects) and two int indexes modified by Interlocked.Decrement (not determine write/read index, but make it unique and correct move).

C# SemaphoreSlim array elements read/write synchronization [closed]

大兔子大兔子 提交于 2020-03-04 20:05:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 days ago . My blocking ring queue don't use lock or Mutex , only two SemaphoreSlim (block than 0 and than max element, so write and read part of array never intersects) and two int indexes modified by Interlocked.Decrement (not determine write/read index, but make it unique and correct move).