semaphore

What is mutex and semaphore in Java ? What is the main difference?

痞子三分冷 提交于 2019-11-26 19:16:07
What is mutex and semaphore in Java ? What is the main difference ? Semaphore can be counted, while mutex can only count to 1. Suppose you have a thread running which accepts client connections. This thread can handle 10 clients simultaneously. Then each new client sets the semaphore until it reaches 10. When the Semaphore has 10 flags, then your thread won't accept new connections Mutex are usually used for guarding stuff. Suppose your 10 clients can access multiple parts of the system. Then you can protect a part of the system with a mutex so when 1 client is connected to that sub-system, no

Java的一些并发包

≡放荡痞女 提交于 2019-11-26 19:07:34
同步容器类 Vector和ArayList : ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问。数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要讲已经有数组的数据复制到新的存储空间中。当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制、移动、代价比较高。因此,它适合随机查找和遍历,不适合插入和删除。 Vector与ArrayList一样, 也是通过数组实现的,不同的是它支持线程的同步 ,即某一时刻只有一个线程能够写Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。Vector与ArrayList的扩容并不一样,Vector默认扩容是增长一倍的容量,Arraylist是增长50%的容量 注意: Vector线程安全、ArrayList Vector.add源码: ​ ArrayList.add源码: ​ 由此,看出,Vectory方法使用synchronized同步函数方法写的,线程同步。 HashMap和HashTable: 1.HashMap不是线程安全的 ,HastMap是一个接口 是map接口的子接口,是将键映射到值的对象,其中键和值都是对象,并且不能包含重复键,但可以包含重复值。HashMap允许null

Is it possible to use mutex in multiprocessing case on Linux/UNIX ?

梦想的初衷 提交于 2019-11-26 18:42:22
This is an interview question. Is it possible to use mutex in multiprocessing case on Linux/UNIX ? My idea: No, different processes have separate memory space. mutex is only used for multithreading. semaphore is used for multiprocessing to do synchronization. right ? Any comments are welcome. thanks Mutual exclusion locks (mutexes) prevent multiple threads from simultaneously executing critical sections of code that access shared data (that is, mutexes are used to serialize the execution of threads). All mutexes must be global. A successful call for a mutex lock by way of mutex_lock() will

JAVA 信号量Semaphore Demo 【转】

家住魔仙堡 提交于 2019-11-26 18:34:06
原文: http://399187879.iteye.com/blog/322647 操作系统的信号量是个很重要的概念,在进程控制方面都有应用。Java 并发库 的Semaphore 可以很轻松完成信号量控制,Semaphore可以控制某个资源可被同时访问的个数,acquire()获取一个许可,如果没有就等待,而release()释放一个许可。比如在Windows下可以设置共享文件的最大客户端访问个数。 Semaphore维护了当前访问的个数,提供同步机制,控制同时访问的个数。在数据结构中链表可以保存“无限”的节点,用Semaphore可以实现有限大小的链表。另外重入锁ReentrantLock也可以实现该功能,但实现上要负责些,代码也要复杂些。 下面的Demo中申明了一个只有5个许可的Semaphore,而有20个线程要访问这个资源,通过acquire()和release()获取和释放访问许可。 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class TestSemaphore { public static void main(String[] args) { //

Is there a Mutex in Java?

不想你离开。 提交于 2019-11-26 18:15:35
Is there a Mutex object in java or a way to create one? I am asking because a Semaphore object initialized with 1 permit does not help me. Think of this case: try { semaphore.acquire(); //do stuff semaphore.release(); } catch (Exception e) { semaphore.release(); } if an exception happens at the first acquire, the release in the catch block will increase the permits, and the semaphore is no longer a binary semaphore. Will the correct way be? try { semaphore.acquire(); //do stuff } catch (Exception e) { //exception stuff } finally { semaphore.release(); } Will the above code ensure that the

Implementing an N process barrier using semaphores

ε祈祈猫儿з 提交于 2019-11-26 18:07:08
问题 I'm currently training for an OS exam with previous iterations and I came across this: Implement a "N Process Barrier", that is, making sure that each process out of a group of them waits, at some point in its respective execution, for the other processes to reach their given point. You have the following ops available: init(sem,value), wait(sem) and signal(sem) N is an arbitrary number. I can make it so that it works for a given number of processes, but not for any number. Any ideas? It's OK

sem_init on OS X

强颜欢笑 提交于 2019-11-26 17:48:59
I am working on some code which uses the pthread and semaphore libraries. The sem_init function works fine on my Ubuntu machine, but on OS X the sem_init function has absolutely no effect. Is there something wrong with the library or is there a different way of doing it? This is the code I am using to test. sem_t sem1; sem_t sem2; sem_t sem3; sem_t sem4; sem_t sem5; sem_t sem6; sem_init(&sem1, 1, 1); sem_init(&sem2, 1, 2); sem_init(&sem3, 1, 3); sem_init(&sem4, 1, 4); sem_init(&sem5, 1, 5); sem_init(&sem6, 1, 6); The values appear to be random numbers, and they do not change after the sem_init

ReleaseSemaphore does not release the semaphore

Deadly 提交于 2019-11-26 17:08:10
问题 (In short: main()'s WaitForSingleObject hangs in the program below). I'm trying to write a piece of code that dispatches threads and waits for them to finish before it resumes. Instead of creating the threads every time, which is costly, I put them to sleep. The main thread creates X threads in CREATE_SUSPENDED state. The synch is done with a semaphore with X as MaximumCount. The semaphore's counter is put down to zero and the threads are dispatched. The threds perform some silly loop and

When should we use mutex and when should we use semaphore

坚强是说给别人听的谎言 提交于 2019-11-26 16:54:46
When should we use mutex and when should we use semaphore ? Annu Gogatya Here is how I remember when to use what - Semaphore: Use a semaphore when you (thread) want to sleep till some other thread tells you to wake up. Semaphore 'down' happens in one thread (producer) and semaphore 'up' (for same semaphore) happens in another thread (consumer) e.g.: In producer-consumer problem, producer wants to sleep till at least one buffer slot is empty - only the consumer thread can tell when a buffer slot is empty. Mutex: Use a mutex when you (thread) want to execute code that should not be executed by

Difference between Counting and Binary Semaphores

◇◆丶佛笑我妖孽 提交于 2019-11-26 15:48:06
问题 What is the difference between Counting and binary semaphore. What I have seen somewhere is that both can control N number of processes which have requested for a resource. Both have taken and Free states. Is there any restriction on how many Resources a Binary semaphore and Counting semaphore can protect? Both allow only One process to use a resource at a time... Is there any other difference? Are the above mentioned properties correct? 回答1: Actually, both types are used to synchronize