-2

I am writing a small program of multiple threads and I am using Semaphore to enforce mutual exclusion.But a problem triggers to my head.

What happens if Semaphore.release is called before Semaphore.acquire ?

mine works fine. is it a bad practice or OK to do ?

user2714949
  • 399
  • 2
  • 3
  • 11
  • Since there are no such methods in `Lock` I'm going to guess you're talking about something else entirely ... a `Semaphore`, for example. And I'm assuming that you actually don't understand what it does if you're asking this question. – Brian Roach Jan 14 '14 at 06:19
  • (So, use a Lock, and read the javadocs). – Brian Roach Jan 14 '14 at 06:26

1 Answers1

1

I think you are talking about the Monitor principle. This is used to avoid deadlocks while accessing data on memory, that is shared with many threads or processes.

If you doing so, please inform yourself about the synchronized block in Java. This ensures that no other thread can access this object at the same time as the thread who is holding the synchronized block in his method body. If the other ones wants to access this data, while you are in synchronzied block, the others have to wait until the current worker thread has leave this state on the relevant object/data.

alpham8
  • 1,314
  • 2
  • 14
  • 32