Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization block  RSS feed

 
Nitasha Gupta
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question in when we use a synchronization block, the object on which the threads have to be synchronized should be static?

For Ex: In this code threads are synchronized on static object. So all the threads have common object monitor.
If I synchronize on object info then why aren't the threads notified when the count becomes equal to 3.

import java.util.Vector;

public class WaitAndNotify_2 extends Thread{

private String info;
static Integer monitor = new Integer(3);
static int count = 0;
static int max = 0;

public WaitAndNotify_2 (String info) {
this.info = info;
}

public void doTheJob() {
synchronized ( monitor ){
System.out.println(info + " is waiting");
count ++;
if ( count == 3 )
monitor.notifyAll();
else
try {
monitor.wait();
sleep(1000);
} catch ( Exception e ){
System.out.println(info +
": IllegalMonitorStateException");
}
System.out.println(info + " is awake!");
}
}


public void run () {
doTheJob();
}

public static void main (String args []) {
new WaitAndNotify_2("first").start();
new WaitAndNotify_2("second").start();
new WaitAndNotify_2("last").start();
}
}
 
Henry Wong
author
Sheriff
Posts: 22818
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question in when we use a synchronization block, the object on which the threads have to be synchronized should be static?

For Ex: In this code threads are synchronized on static object. So all the threads have common object monitor.
If I synchronize on object info then why aren't the threads notified when the count becomes equal to 3.


It actually doesn't have to be static, the key is that threads must use the same object, in order to send notifications to each other. So... you can use the "info" reference, provided that they are set to the same object by the constructor.

Henry
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nitasha Gupta:

My question in when we use a synchronization block, the object on which the threads have to be synchronized should be static?

For Ex: In this code threads are synchronized on static object. So all the threads have common object monitor.



In this case, all threads will have same lock because there is only one Integer object as it is static. All three thread instances share same static Integer instance.


Originally posted by Nitasha Gupta:

If I synchronize on object info then why aren't the threads notified when the count becomes equal to 3.



String object info is non-static. So there will be three String instances here each is associated with one thread instance.

Since String objects are different so their lock will be different. All the three threads will have different locks here.


Regards


Naseem
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitasha Gupta:
My question in when we use a synchronization block, the object on which the threads have to be synchronized should be static?

...


Not necessary to be static. Yet they should be the _same_ object. In your example you have created 3 different objects. Notification of an object only works for waiters on that object. You are notifying on 3 seperate objects and waiting on 3 seperate objects.

For your example, try passing the same object into the constructor of your three WaitAndNotify objects, and wait/notify on that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!