• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Monitors

 
syruss kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Can any one explain about monitors and semapores in java thread ?i have read some articles in the web regarding this.But i want to know

1.How can i monitor a object manually ?
2.what is the role of semapore ?how can we use that in our program


Thanks in advance


 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
syruss kumar wrote:Hi ,

Can any one explain about monitors and semapores in java thread ?i have read some articles in the web regarding this.But i want to know

1.How can i monitor a object manually ?


What do you mean by monitor an object manually? What do you want to do?


2.what is the role of semapore ?how can we use that in our program


A Semaphore is a permissions system. It allows you to let a fixed, and controlled number of Threads to access a portion of code. As an example, lets say you have an image processing application. You want to make most of it to work run in parallel - ie multiple actions can run at the same time. But some of the parts of code - those that need to make multiple memory-copies of the image let's say - take lots of memory, so you want to make sure no more than 3 of them run at one time. You can use a Semaphore with 3 permissions to control access to these portions of code. Once the third process starts, then a fourth process needs to wait for one to complete.

Semaphores have also been used as a sort of 'wait' mechanism where the notifier does not need to synchronize on the object, which prevents possible deadlock situations. Consider this code. The 'waiter' is just waiting to be allowed to proceed. The 'notifier' tells the waiter when it can go.


This can cause deadlock problems especially if the notifier is locked on another object already. You could use a semaphore in its place:


Note that the two code snippets aren't exactly equivalent. In the synchronized code if the notifier happens before the waiter code then the waiter will wait until the next notifier. In the Semaphore code in the same situation the one permit provided by the notifier will exist when the waiter gets there at some later point, meaning if the notifier happens before the waiter code, the waiter code can continue without pause.
 
Henry Wong
author
Marshal
Pie
Posts: 21414
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1.How can i monitor a object manually ?


A "monitor" is a term that is somewhat outdated. Some people use it to mean an object that does synchronization. Others use it to refer to condition variables (the wait() and notify() methods). The best definition is probably a combination of both.

Regardless, it is not really used to "monitor a object" -- whatever that means.

2.what is the role of semapore ?how can we use that in our program


A semaphore is a special type of synchronization. Think of it as synchronization for a group of resources -- if the resource was of size one, then it would be like (equivalent to) synchronization.

See the java.util.concurrent.Semaphore class for Java's implementation of the semaphore.


Anyway, it may be a good idea to start with the Sun tutorial on threading ...

http://java.sun.com/docs/books/tutorial/essential/concurrency/

Henry
 
syruss kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,


Thanks a lot steve and henry.Really its helped me a lot .

Can you explain the role of MUTEX?
i found some articles in web that MUTEX is a key for a object . and as you said semapore is a permission system.it may contain number of keys for that object .How these to works together?

Thanks in advance

 
Henry Wong
author
Marshal
Pie
Posts: 21414
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain the role of MUTEX?


The term "mutex" is short for mutually exclusive lock.... basically, it is the low level locking mechanism that many other locking mechanism can be based on.

It is *not* a term that is used in Java much. Generally, when the term "sychronization" is used -- it involves a mutex. Java also have the Lock class (interface) which provides synchronization semantics (ie. mutex), as a class... so instances of the Lock class are mutexes.

I really really recommend that you go through the tutorial instead of just reading random pages on the internet -- as you seem to be mixing terminology from many different threading systems. And it is probably easier to learn one threading system at a time.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21414
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found some articles in web that MUTEX is a key for a object . and as you said semapore is a permission system.it may contain number of keys for that object .How these to works together?


To start, Java originally only had synchronization (and wait and notify), so semaphores didn't really exist in the early days. Back then, if you wanted a semaphore, you had to implement it youself, by creating a class -- using synchronization, of course.

With Java 5, the concurrent libraries were added. And included with them were the Lock (and Condition) class, which implemented synchronization as a class, and the Semaphor class, which implement semaphores.

In theory, it is possible to use semaphores like mutexes, and it is possible to implement a semaphore class using mutexes -- and IMO, there is really no one right way to use them.

Henry

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic