• 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

Thread Questions

 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am not able to understand the behaviour of below mentioned codes
Pleasse explain

Code A -

public class Run {
public static void main(String[] args) {
System.out.print("A ");
final Object test = new Object();
synchronized (test) {
test.notifyAll();
}
System.out.println("B");
}
}
Output is - “A B”


========================

Code B -

public class Run {
public static void main(String[] args) {
System.out.print("A ");
synchronized (new Object()) {
new Object().notifyAll();
}
System.out.println("B");
}
}
Output - It prints “A ” and an exception is thrown





 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are no other threads waiting for the monitor on your synchronized object, so main() continues to execute. When notifyAll() is called there are no other threads in the waiting state. So, the scheduler runs a thread in the Runnable state. main() is in the Runnable state.

In B,



You must call notifyAll() on a synchronized object. An IllegalMonitorStateException should have been thrown.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic