semaphore

Semaphore可以控制并发访问的线程个数

主宰稳场 提交于 2019-12-04 19:58:18
public class SemaphoreTest { //信号量,只允许 3个线程同时访问 private static Semaphore semaphore = new Semaphore(3); public static void main(String[] args) { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { final int num = i; executorService.submit(new Runnable() { @Override public void run() { showLog(num); } }); } executorService.shutdown(); } private static void showLog(int num) { //获取许可 try { semaphore.acquire(); //执行 System.out.println("Accessing: " + num); Thread.sleep(2000); //休眠2秒 } catch (InterruptedException e) { e.printStackTrace(); }finally { //释放

When should I use semaphores?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 19:13:58
问题 When would one use semaphores ? Only example I can think of is limiting the number of threads accessing the same data/code simultaneously... Any other scenarios in which semaphores would be the best solution ? 回答1: Semaphores might be appropriate for signaling between processes. For multithreaded programming, semaphores should be avoided. If you need exclusive access to a resource, use mutex. If you need to wait for a signal, use condition variable. Even the most often mentioned case of a

使用semaphore写一个显示锁

只愿长相守 提交于 2019-12-04 18:08:13
public class SemaphoreLock { private final Semaphore semaphore = new Semaphore(1); public void lock() throws InterruptedException { semaphore.acquire(); } public void unlock(){ semaphore.release(); } public static void main(String[] args) { SemaphoreLock lock = new SemaphoreLock(); for(int i=0; i<2; i++){ new Thread(()->{ try { System.out.println(Thread.currentThread().getName() + " is running "); lock.lock(); System.out.println(Thread.currentThread().getName() + " get the lock "); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } System

Operating System: Semaphore

亡梦爱人 提交于 2019-12-04 17:53:31
本文主要参考《计算机操作系统(第四版)》(西安电子科技大学出版社)以及清华大学操作系统公开课(向勇、陈渝),整理操作系统的基本概念,供自己复习查阅。 信号量机制 进程控制中最重要的一部分便是协调好进程的并发,控制进程同步,最具体的体现就是处理临界资源。信号量机制便广泛应用在临界资源处理方面。 信号量的分类与发展 信号量最初的定义是表示资源的 数目 。 整型信号量 顾名思义,这种信号量就是用一个整型量s表示某临界资源的数目。在初始化时,把s初始化为资源的数目,并限制只能通过原子操作 wait(s) 和 signal(s) 来访问,这两个操作通常也被称为 P 、 V 操作。可代码表示如下: wait(s) { while(s <= 0); --s; } signal(s) { ++s; } 记录型信号量 可以看到,在整型信号量中,只要申请不到资源,就会不断调试,并不符合“让权等待”原则(即让进程处于“忙等”状态),而记录型信号量就可以解决这个问题。 在记录型信号量中,简单的整型量s被扩充为记录型结构体,里面额外保存了一个进程表指针,用于链接所有等待进程。 struct ProcessControlBlock; using PCB = ProcessControlBlock; struct Semaphore { int value; // 资源数目 PCB* processList;

What happens when two processes are trying to access a critical section with semaphore = 0?

与世无争的帅哥 提交于 2019-12-04 17:24:16
In my code I do the following initialization : struct PipeShm myPipe = { .init = 0 , .flag = FALSE , .mutex = NULL , .ptr1 = NULL , .ptr2 = NULL , .status1 = -10 , .status2 = -10 , .semaphoreFlag = FALSE }; int initPipe() { if (!myPipe.init) { myPipe.mutex = mmap (NULL, sizeof *myPipe.mutex, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (!sem_init (myPipe.mutex, 1, 0)) // semaphore is initialized to 0 { myPipe.init = TRUE; } else perror ("initPipe"); } return 1; // always successful } I can have multiple processes that can be invoked from main() (note the fork ) . Thanks AFAICS

How does threads sleep with interrupt disabled?

本小妞迷上赌 提交于 2019-12-04 17:18:54
I am trying to understand how the code below works. This is straight out of my profs lecture slides. This P() and V() function is the part of semaphore implementation in the OS that we use in class (OS161). I think you might need understanding of the OS161 to answer my question, since its widely used, hopefully some one can answer this questions. My understanding of this code with lecture notes: X:Flow of the P() function 1. When a thread call P(), we disable interrupt 2. check if we have any resources available on sem->count 3.a) if count is 0 then we go to sleep 3.b) if count != 0 then we

correct way to wait for dispatch_semaphore in order to wait for many async tasks to complete

你离开我真会死。 提交于 2019-12-04 17:02:05
I have an asynchronous method longRunningMethodOnObject:completion: this method receives an object of type 'Object' - does work with its data and then calls the completion handler. I need to call many different "longRunningMethods" and wait for all to complete. I would like all of the "longRunningMethodOnObject" to run asynchronously (parallel) to each other in the "for" loop. (I am not certain if the "longRunningMethodOnObject" runs in serial to each other but this is more of a general question) I'm not sure I have created a proper semaphore and would appreciate an explanation on the proper

Java线程--Semaphore使用

北城余情 提交于 2019-12-04 15:48:28
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11872132.html Java线程--Semaphore使用 Semaphore是信号量, 也叫许可证管理器, 类似于取票窗口, 办事窗口, 打饭窗口等等这种情况, 只有排队等到了这样才算拿到了信号量, 拿到了许可证 . package concurrent.semaphore; import java.util.concurrent.Semaphore; /** * 信号量 , 许可证管理器 main 测试类 */ public class MainTest { public static void main(String[] args) throws InterruptedException { /** * 定义2个取票窗口, 公平的排队取票 */ Semaphore ticketWindow = new Semaphore(2, true); Thread[] threads = new Thread[10]; for (int i = 0 ; i < 30; i++) { /** * 前面十个小伙伴坚持排队 */ if (i < 10) { new Thread(new Man("坐火车良民" + i, ticketWindow, 0)).start(); }

How does this implementation of semaphore work?

自古美人都是妖i 提交于 2019-12-04 15:11:34
问题 Merry Xmas! I'm reading the The Little Book of Semaphores. There is an implementation of semaphores in C in the book that I don't completely understand. See below for code. There is this wakeups variable. The author explains: wakeups counts the number of pending signals; that is, the number of threads that have been woken but have not yet resumed execution. The reason for wakeups is to make sure that our semaphores have Property 3, described in Section 4.3 and Property 3: if there are threads

How to Implement Semaphores in iOS Application?

夙愿已清 提交于 2019-12-04 14:01:39
问题 Is it possible to implement Counting Semaphore in ios application? 回答1: Yes, it is possible. There are quite a few synchronization tools available: @synchronized NSLock NSCondition NSConditionLock GCD semaphores pthread locks ... I'd suggest reading "Threading Programming Guide" and asking something more specific. 回答2: Like this: dispatch_semaphore_t sem = dispatch_semaphore_create(0); [self methodWithABlock:^(id result){ //put code here dispatch_semaphore_signal(sem); [self methodWithABlock: