• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronization block

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic