Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use semaphore in JAVA?  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When There is synchronized block in JAVA, Why do I need to use semaphore and write code manually?
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!