Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does synchronized()/wait()/notifyAll() do in Java? [duplicate]

Possible Duplicate:
Java Synchronization

I'm reading the book Beginning Android Games.

It uses synchronized() a lot but I don't really understand what it does. I haven't used Java in a long time and I'm not sure if I ever used multithreading.

In the Canvas examples it uses synchronized(this). However in the OpenGL ES example, it creates an Object called stateChanged and then uses synchronized(stateChanged). When the game state changes it calls stateChanged.wait() and then stateChanged.notifyAll();

Some code:

    Object stateChanged = new Object();      //The onPause() looks like this:     public void onPause()         {             synchronized(stateChanged)             {                 if(isFinishing())                     state = GLGameState.Finished;                 else                     state = GLGameState.Paused;                  while(true)                 {                     try                     {                         stateChanged.wait();                         break;                     } catch(InterruptedException e)                     {                     }                 }             }         } //The onDrawSurface looks like this: public void onDrawFrame(GL10 gl)     {         GLGameState state = null;         synchronized(stateChanged)         {             state = this.state;         }          if(state == GLGameState.Running)         {          }          if(state == GLGameState.Paused)         {             synchronized(stateChanged)             {                 this.state = GLGameState.Idle;                 stateChanged.notifyAll();             }         }          if(state == GLGameState.Finished)         {             synchronized(stateChanged)             {                 this.state = GLGameState.Idle;                 stateChanged.notifyAll();             }         }     }  //the onResume() looks like this: synchronized(stateChanged)         {             state = GLGameState.Running;             startTime = System.nanoTime();         } 
like image 418
Tiago Costa Avatar asked Aug 02 '11 15:08

Tiago Costa


People also ask

What is the purpose of wait () notify () notifyAll () methods?

The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object's monitor. The notifyAll() method wakes up all threads that are waiting on that object's monitor.

Why wait () notify () and notifyAll () methods have to be called from synchronized method or block illustrate with an example?

Calling notify() or notifyAll() methods issues a notification to a single or multiple threads that a condition has changed and once the notification thread leaves the synchronized block, all the threads which are waiting for fight for object lock on which they are waiting and lucky thread returns from wait() method ...

What does wait () do in Java?

wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

What is notifyAll method in Java?

notifyAll() wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods. The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object.


2 Answers

The synchronized keyword is used to keep variables or methods thread-safe. If you wrap a variable in a synchronized block like so:

synchronized(myVar) {     // Logic involing myVar } 

Then any attempts to modify the value of myVar from another thread while the logic inside the synchronized block is running will wait until the block has finished execution. It ensures that the value going into the block will be the same through the lifecycle of that block.

like image 195
Jason Robinson Avatar answered Oct 01 '22 14:10

Jason Robinson


This Java Tutorial can probably help you understand what using synchronized on an object does.

When object.wait() is called it will release the lock held on that object (which happens when you say synchronized(object)), and freeze the thread. The thread then waits until object.notify() or object.notifyAll() is called by a separate thread. Once one of these calls occurs, it will allow any threads that were stopped due to object.wait() to continue. This does not mean that the thread that called object.notify() or object.notifyAll() will freeze and pass control to a waiting thread, it just means these waiting threads are now able to continue, whereas before they were not.

like image 39
tdashroy Avatar answered Oct 01 '22 14:10

tdashroy