| Author |
When to use semaphore in JAVA?
|
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
Java 5 comes with semaphore implementations in the java.util.concurrent package so you don't have to implement your own semaphores. Still, it can be useful to know the theory behind their implementation and use.
http://tutorials.jenkov.com/java-concurrency/semaphores.html
Java's monitors and wait sets are versatile enough to solve any synchronization problem but safe enough to keep most programmers out of trouble.
http://www.javaworld.com/javaworld/javaqa/1999-11/02-qa-semaphore.html
http://www.ibm.com/developerworks/library/j-thread.html
Code from this site : http://www.ibm.com/developerworks/library/j-thread.html
1-In that code. even If we don't use the counter, only one thread can access to release method at same time. Why does he use counter
2-When to use semaphore in JAVA?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
Your topic starts off about asking about the Java 5 semaphore.
Then, you follow up with a ton of examples that implement their own semaphore. So, are you asking about the Java 5 semaphore? Or about the two or three implementations that you linked to?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
To answer your question...
abalfazl hossein wrote:
1-In that code. even If we don't use the counter, only one thread can access to release method at same time. Why does he use counter
2-When to use semaphore in JAVA?
There is no counter, or semaphore, being used in the example that you provided. But yes, a semaphore is generally used when there is a limited number of resources, and the limit is generally more than one (but one is okay too) -- with it more than one thread can be active at a time.
Henry
|
 |
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
|
When There is synchronized block in JAVA, Why do I need to use semaphore and write code manually?
|
 |
Adam Smolnik
Ranch Hand
Joined: Apr 15, 2009
Posts: 63
|
|
Hey.
Have a look at a similar topic with explanation "why some concurrence patterns like ReentrantLock and Semaphore over synchronized construction":
http://www.coderanch.com/t/480872/Threads-Synchronization/java/using-ReentrantLock-model-Semaphore
and in particular:
http://www.ibm.com/developerworks/java/library/j-jtp10264/index.html
Adam
|
SCJP, SCWCD, SCBCD, SCDJWS
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
abalfazl hossein wrote:When There is synchronized block in JAVA, Why do I need to use semaphore and write code manually?
You can make the same argument for not having any of the collection classes. Since Java has arrays, and references, why do you need ArrayList and LinkedList? You can implement everything without it. But why? Why would you want to implement something when you don't have to?
Same thing for the java.util.concurrent.Semaphore class. Its implementation allows more than one thread to be running concurrently. You can implement something similar by easily implementing your own Semaphore class from the built in synchronization, but why? why not just use the built in semaphore class when you need it.
Henry
|
 |
 |
|
|
subject: When to use semaphore in JAVA?
|
|
|