File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes synchronization understanding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronization understanding" Watch "synchronization understanding" New topic
Author

synchronization understanding

sandipan chakrabarti
Greenhorn

Joined: Mar 25, 2007
Posts: 6
import java.util.concurrent.atomic.AtomicBoolean;
class SynchronizedCounter {
private int c = 0;
public synchronized void increment() {
try {
System.out.println("11111111");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public synchronized void decrement() {
c--;
}

public synchronized int value() {
return c;
}
}

public class test2 extends Thread{
static SynchronizedCounter c = new SynchronizedCounter(); ;
public void run(){
c.increment();
}
public static void main(String[] args) {
test2 t = new test2();
test2 t1 = new test2();
t.start();
t1.start();
}
}

The result-----

11111111
11111111

Q if the SynchronizedCounter methods are synchronized then I can assume that when one thread enters the critical section in increment() then the other thread is blocked. then why 11111 is printed twice instead of once??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Q if the SynchronizedCounter methods are synchronized then I can assume that when one thread enters the critical section in increment() then the other thread is blocked. then why 11111 is printed twice instead of once?


The other thread *is* blocked -- because it is waiting to acquire the lock. However, once the wait() method is called, it will release the lock, allowing the other thread to acquire the lock.


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amitt jainn
Greenhorn

Joined: Jan 21, 2008
Posts: 4
I am totally agreed with Henry..
explaning it further syncing with the question.



actually when we call wait() on an object, it relinqushes the monitor it holds and goes to the waiting thread pool of that object ('this' in this case). Now this thread will not gain its ready state until a notify is called on this object (this in our example).

Now since the thread has already released the monitor another thread can very smoothly obtain monitor on this object.

That is why your program returned

11111111
11111111

Regards
Amit
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Howdy sandipan chakrabarti ,

Kindly make use of the Code Tags when posting the java code


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronization understanding