Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds


  Asked By: Bogart    Date: Feb 23    Category: Java    Views: 703

I'm not being clear about the synchronization problem.
I know what is critical section and stuff and i know that if we put
the critical section code in a block like

all the threads trying to execute something on object will do it one
by one... simple uptill now

Then where the hell do Object.wait() and Object.notify() come in and
why we use them????

I want to know are synchronized(){...} block and combination of wait
and signal, rather notify alternate methods of applying mutual
exclusion? or there is some other issue, can any1 elaborate on that



4 Answers Found

Answer #1    Answered By: Estella Mitchell     Answered On: Feb 23

Java language is operated on multithreading level
access. Synchronized wait, notify, and notifyAll
helping to protect against concurrently access
problems which occured when your logic is not sound.
It allows you to chose what thread to go first and so

Sorry, you want elaborated and I just gave you a
summary. This one will take very long and occupy a
lot of bandwidth. Would you settle for a link?


Answer #2    Answered By: Felicia Hill     Answered On: Feb 23

See if I can help with a brief picture here.

Imagine you are developing a web application using servlet API, in
the scenario that many of its services require access to certain
limited resource. In this case a database, for example. To access
this database, you need to provide connection objects. And for
better/faster response on user query, these connection objects should
be pre-created and pooled when the server first initialized. The
actual number of these pooled objects depends on what database you
are using. Microsoft Access, for instance, can provide up to 32
connections simultaneously (but note that it's redundant to do this
with Microsoft Access since it has its own internal connection pool).
As the web app running and multiple servlets dispatched, it is
possible that in the peak time the number of servlet instances --
that require access to the database -- is exceeding the number of
available connection objects. For this matter, you'll need to create
and deploy, say, a connection manager. Any servlet instance requiring
access to the database should go to the connection manager which in
turn checks any available connection object. If does available, it is
assigned to the servlet to work with. Otherwise, the sevlet(s) must
_wait_. When some other servlet is done working with a connection, it
should be returned to the conection manager which in turn _notify_
the waiting servlets that a connection is available (again). One of
these servlet instances will then -- in undetermined order -- be
picked to proceed with it.

Someone can create similar application with mere syncronized block,
of course. But there would be a great performance loss since, as I
can imagine, it may only utilize just one connection object  at a time.

Answer #3    Answered By: Corinne Rogers     Answered On: Feb 23

If you read the online documents, then you must spot
out Java is operated on a multithreading info access

> I have used a synchronized block  to read and update
> the integer, and
> the result is perfect. If i dont use this
> synchronized(){...} block,
> it does'nt work at all ,(gives wrong answer)..

The synchronized keyword help to ensure one thread
running at a time of the given code. But it does not
lock out all other synchronized methods  running in the
same object  nor give you much communication between

> If I have solved the problem  and its working fine
> now, WHY DO I NEED
> TO CALL wait  ON some object or something

wait() and notify() and notifyAll() help the threads
to communicate with each others.

> .. And if i
> do, where in the
> following code  do i need to use them

Your program does not required for it yet. =)
The sequence is notifyAll() or notify() before wait().

You already used join() - causes the compiler to wait
for the thread to die out. You could have used wait()
or sleep(), etc... to make your logic more
complicated. =)

Try to follow the thread life cycle schema, you can go
no wrong. I think Deitel & Deitel's Java How to
Program has the best Thread Life Cycle Schema.

I think your program is not finished or it is just a
part of a multiple programs.

Answer #4    Answered By: Agatha Miller     Answered On: Feb 23

I believed that I only answered the static
synchronized() situation.

For the non-static synchronized(), any number of
threads may run the given at the same time, as long as
each of the threads  calls the method on a different

Didn't find what you were looking for? Find more on Synchronization Or get search suggestion and latest updates.